#### Prev Next

An operator is used to perform an operation over one or more operands. Operators in Java are classified as,

- Assignment Operator
- Arithmetic Operator
- Unary Operator
- Relational Operator
- Logical / Conditional Operator
- Bitwise Operator

**1. Arithmetic Operators:**

They are used to perform arithmetic operations over two operands.

Operator |
Description |

+ | Adds both of the operands and returns the result. Example: int a = 10, b=2, c; c = a + b; //c = 12 |

– | Subtracts the right hand side operand from the left hand side operand. Example: c = a – b; //c = 2 |

* | Multiplies both the operands and returns the result. Example: c = a * b; //c = 20 |

/ | Divides left hand side operand by the right hand side operand. Example: c = a / b; //c =5 |

% | Divides left hand side operand by the right hand side operand and returns the reminder. Example: c = a % b; //c = 0 |

**2. Unary Operators:**

They are used to perform operations over a single operand.

Operator |
Description |

+ (Unary plus) | For referring +ve values. By default all values are positive. Example: int a = +5; |

– (Unary Minus) | Negates a value. Example: int a = -5; |

++operand (Pre_Increment)operand++ (Post_Increment) |
First increments the operand value by 1 and then the new value is used in the expression. The value of the operand is first used in the expression and then incremented by 1. Example: int a = 5; int b = ++a + 1; /* a = 6, b = 7 */ b = a++ +1; /* a = 6, b = 7. After this statement, ‘a’ becomes 7 */ |

–operand (Pre_Decrement)operand– (Post_Decrement) |
First decrements the operand value by 1 and then the new value is used in the expression. The value of the operand is used in the expression first and then decremented by 1. Example: int a = 5; int b = ++a + 1; /* a = 6, b = 7 */ b = a++ +1; /* a = 6, b = 7. After this statement ‘a’ becomes 7 */ |

! (Logical NOT operator) |
Complement operator. It inverts the logical state of an operand. Example: boolean flag = !true; /* flag will contain false */ !(A= =B) /* inverts the result of the expression */ |

**3. Relational Operators:**

They are used to compare the values of two operands.

Operator |
Description |

== | Checks whether values of both side of the operands are equal. Example: int a = 10, b = 10; boolean var = (a == b);Used in loops as conditional expression: if (a == b) { System.out.println(“values are equal”); } |

!= | Checks whether values of both the operands are unequal. Example: check = (a! = b); // true if ‘a’ is not equal to ‘b’ |

> |
Checks whether the operand in the left hand side is greater than the right hand side operator. Example: int a = 5, b = 2; (a > b) would return true. |

< | Checks whether the operand in the left hand side is less than the right hand side operator. Example: int a = 5, b = 2; (a < b) would return false. |

>= | Checks whether the operand in the left hand side is greater than or equal to the right hand side operator. Example: int a = 2, b = 2; (a >= b) would return true. |

<= | Checks whether the operand in the left hand side is less than or equal to the right hand side operator. Example: int a = 3, b = 2; (a <= b) would return false. |

**4. Logical / Conditional Operators:**

Operator |
Description |

&& (Logical AND) |
If both the operands are true, then the condition evaluates to true. Otherwise as false. Consider X = true, Y = true. (X && Y) evaluates to true |

| | (Logical OR) |
If any of the operand is true, then the condition evaluates to true. Otherwise as false. Consider X = true, Y = true (X | | Y) evaluates to true |

? : (Ternary operator) |
This operator requires 3 operands. It represents short hand form of ‘if else’ loop.
Syntax: Example: |

**5. Bitwise Operators:**

Operator |
Description |

~ (Bitwise complement) |
This operator inverts a bit pattern making 0’s to 1 and 1’s to 0. |

& (Bitwise AND) |
Performs bitwise AND operation. Example: consider x = 15, y = 6 (x = 0000 1111, y = 0000 0110) x&y = 6 (0000 0110) |

| (Bitwise inclusive OR) |
Performs bitwise OR operation. Example: consider x = 14, y = 12 (x = 0000 1110, y = 0000 1100) x|y = 14 (0000 1110) |

^ (Bitwise exclusive OR) |
Performs bitwise XOR operation. Example: consider x = 15, y = 12 (x = 0000 1111, y = 0000 1100) x^y = 3 (0000 0011) |

<< (Left shift) |
The operand’s value is moved left by the number of bits specified in the right side of the operator <<. Example: consider x = 7 (0000 0111) x<<2 = 24 (0001 1100) |

>> (Right shift) |
The operand’s value is moved right by the number of bits specified in the right side of the operator >>. Example: consider x = 7 (0000 0111) x>>2 = 1 (0001) |

>>> (Zero fill right shift) |
Same as right shift, fills zero in the shifted places. Example: consider x = 7 (0000 0111) x>>2 = 1 (0000 0001) |

**6. Assignment Operator:**

The Assignment operator ‘=’ is used to assign the value on the right hand side to the operand on the left.

**Example:**

int i = 1;

The value 1 is assigned to the variable ‘i’.

Operator |
Description |

+= | Performs addition & assigns the result to the left hand side. Example: x+=2; //(x = x+2) |

-= | Performs subtraction and assigns the result to the right hand side. Example: x-=2; //(x = x-2) |

*= | Performs multiplication and assigns the result to the right hand side. Example: x*=2; //(x = x*2) |

/= | Performs division and assigns the result to the right hand side. Example: x/=2; //(x = x*2) |

%= | Performs division and assigns the remainder to the right hand side. Example: x%=2; //(x = x%2) |

<<= | Performs left shift and assigns the result to the right hand side. Example: x<<=2; //(x = x<<2) |

>>= | Performs right shift and assigns the result to the right hand side. Example: x>>=2; //(x = x>>2) |

&= | Performs Bitwise & and assigns the result to the right hand side. Example: x&=2; //(x = x&2) |

^= | Performs Bitwise ^ and assigns the result to the right hand side. Example: x^=2; //(x = x^2) |

|= | Performs Bitwise | and assigns the result to the right hand side. Example: x|=2; //(x = x|2) |

**The instanceof Operator (Type comparison operator):**

The instanceof operator is used to check whether an object is an instance of a particular type (class, array).

**Example:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Class Employee { int eid; String name; public void display() { System.out.println(“ID:”+eid); System.out.println(“Name:”+name); } public static void main(String args[]) { Employee e1 = new Employee(); System.out.println(Is e1 object of Employee:’+(e1 instanceof Employee)); } } |

**Output:**

Is e1 object of Employee: true

**Operator Precedence:**

Operator |
Associativity |

() (Paranthesis) [] (Array Subscript) .(dot) (Member selection) |
Left to right |

++ (Unary pre/post increment) — (Unary pre/post decrement) ! (Unary negation operator) ~ (Bitwise complement) |
Right to left |

* (Multiplication) / (Division) % (Modulus) |
Left to right |

+ (Addition) – (Subtraction) |
Left to right |

>> (Right shift) >>> (Zero right shift) << (Left shift) |
Left to right |

> (Greater than) > (Less than) >= (Greater than or equal to) <= (Less than or equal to) |
Left to right |

== (Equal to) != (Not equal to) |
Left to right |

& (Bitwise AND) ^ (Bitwise XOR) | (Bitwise OR) && (Logical AND) || (Logical OR) |
Left to right |

?: (Ternary) | Right to left |

= (Assignment) += (Addition Assignment) -= (Subtraction Assignment) *= (Multiplication Assignment) /= (Division Assignment) %= (Modulus Assignment) >>= (Bitwise Right shift Assignment) <<= (Bitwise Left shift Assignment) &= (Bitwise AND Assignment) ^= (Bitwise XOR Assignment) |= (Bitwise OR Assignment) |
– |

#### Prev Next

1. Online Compilers - Compile & Run your C/C++ programs:

## ✅ Online Compilers

2. Ask your questions or clarify your/others doubts from,

## ✅ Online Discussion Forums by Fresh2Refresh.com