# Operators in Java

Operators are used to perform following operations- arithmetic operations (addition, subtraction, multiplication & division), compare values of two variables, to apply bitwise operations, etc.

There are seven types of operators in Java which are explained below-

## Arithmetic Operators

If you want to perform basic algebraic operations like +, -, * and / in Java then use arithmetic operators. Java provides seven arithmetic operators which are explained below-

Operator Usage Description
+ value1 + value2 Adds value1 and value2.
- value1 - value2 Subtract value2 from value1.
++ value1 ++ value2 Increment value1 by 1.
-- value1 -- value2 decrement value1 by 1.
* value1 * value2 Multiplies value1 by value2.
/ value1 / value2 Divides value1 by value2.
% value1 % value2 Calculates the remainder of dividing value1 by value2.

## Relational Operators

It is also known as a comparison operators. It compares the values of two operands and returns a boolean value- true or false.

Operator Usage Description
== value1 == value2 if value1 and value2 are equal, then the result is true otherwise false.
!= value1 != value2 if value1 and value2 are not equal, then the result is true otherwise false.
> value1 > value2 if value1 is greater than value2, then the result is true otherwise false.
>= value1 >= value2 if value1 is greater or equal than value2, then the result is true otherwise false.
< value1 < value2 if value1 is less than value2, then the result is true otherwise false.
<= value1 <= value2 if value1 is less or equal than value2, then the result is true otherwise false.

## Bitwise Operators

Java provides six bitwise operators to manipulate the bits of integer value. You can apply these operators on byte, short, int, long, and char value.

Operator Usage Description
Bitwise AND(&) value1 & value2 It performs AND logic on numbers at bit level.
Bitwise OR(|) value1 | value2 It performs OR logic on numbers at bit level.
Bitwise XOR(^) value1 ^ value2 It performs XOR logic on numbers at bit level.
Bitwise Inversion(~) ~value1 This operator inverts the value of each bit of the operand.
Right shift(>>) value1 >> value2 It right shifts the bits of values1 by values2 times.
Left shift(<<) value1 << value2 It left shifts the bits of values1 by values2 times.

Bitwise AND, OR, and XOR follows the truth table while performing operation-

x y x & y x | y x ^ y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

## Logical Operators

Logical operators are also known as short-circuit logical operators. There are three logical operators which are explained below-

Operator Usage Description
Logical AND(&&) expression1 && expression2 It checks the second operand when the first operand turns out to be true. If the first operand evaluates to false, then the second operand is not even checked.
Logical OR(||) expression1 || expression2 It checks the second operand when the first operand is false. If the first operand evaluates to true, then the second operand is not even checked.
Logical NOT(!) !expression1 If an expression gives true, then this operator would inverse it and produces false. Similarly, if an expression gives false, then this operator would make it true.

## Assignment Operators

The assignment operator is used to assign the value to a variable. There is a variation in assignment operator when it is comined with other operators. Such a variation is called shortcut assignment operators.

Let's see how we can use these operators-
a = a + b;

With shortcut assignment operator, you can write like this-
a += b;

Operator Usage Description
+= value1 += value2 value1 = value1 + value2
-= value1 -= value2 value1 = value1 - value2
*= value1 *= value2 value1 = value1 * value2
/= value1 /= value2 value1 = value1 / value2
%= value1 %= value2 value1 = value1 % value2
&= value1 &= value2 value1 = value1 & value2
|= value1 |= value2 value1 = value1 | value2
^= value1 ^= value2 value1 = value1 ^ value2
<<= value1 <<= value2 value1 = value1 << value2
>>= value1 >>= value2 value1 = value1 >> value2
>>>= value1 >>>= value2 value1 = value1 >>> value2

## Ternary Operators

You can think it as a shortcut of an if-else statement. It is called ternary operator because it involves the three operands. Consider the following if-else statement-

```if(a>b)
c = 10;
else
c = 12;```

With ternary operator, you can do it in one line. The equivalent of above if-else statement is

`c = (a > b) ? 10 : 12;`