Java - Operators
Java provides a rich set of operators environment.
Java Operators can be divided into following categories.
- Arithmetic Operators
- Relation Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
These operators are used in mathematical expressions.
Let's suppose the value of X = 10 and Y = 5
|+||Add two operands||X+Y gives 15|
|-||Subtract second operand from first||X-Y gives 5|
|*||Multiply two operands||X*Y gives 50|
|/||Divide first operand by second||X/Y gives 2|
|%||Remainder of division||X%Y gives 0|
|++||Increment operator by 1||X++ gives 11|
|--||Decrement operator by 1||Y-- gives 4|
|==||Check if values of operands are equal, if yes then condtion is true||X==Y gives false|
|!=||Check if values of operands are not equal, if yes then condtion is true||X!=Y gives true|
|>||Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.||X>Y gives true|
|<||Checks if the value of left operand is smaller than the value of right operand, if yes then condition becomes true.||X<Y gives false|
|>=||Checks if the value of left operand is greater than or equal to value of right operand, if yes then condition becomes true.||X>=Y gives false|
|<=||Checks if the value of left operand is smaller than or equal to the value of right operand, if yes then condition becomes true.||X<=y gives true|
Suppose X = true and Y = false
|&&||Logical AND||X&&Y is false|
|||||Logical OR||X||Y is true|
|!||Logical NOT||!Y is true|
Bitwise operator works on bits. Operation is performed bits by bits.
Representation of X(10) in binary : 1010
Representation of Y(5) in binary : 0101
Now when Bitwise AND(&) operation is performed it will give:0000, therefor output will be 0.
When Bitwise OR(&) operation is performed it will give:1111, therefor output will be 15.
|&||Bitwise AND||X&Y gives 0|
||||Bitwise OR||X|Y gives 15|
|^||Bitwise XOR||X^Y gives 12|
|<<||Left Shift (shift bits in left by given number)||X<<2 gives 640|
|>>||Right Shift (shift bits in right by given number)||Y>> gives 2|
|=||Assigns values from right side operands to left side operand.||Z = X + Y|
|+=||Add right operand to left operand and assigns the value to right operand||Z+=X, means Z=Z+X|
|-=||Subtracts right operand from the left operand and assign the result to left operand.||Z-=X, means Z=Z-X|
|*=||Multiplies right operand with the left operand and assign the result to left operand.||Z*=X, means Z=Z*X|
|/=||Divides left operand by right operand and assign the result to left operand.||Z/=X, means Z=Z/X|
|%=||Takes modulus using two operands and assign the result to left operand.||Z%=X, means Z=Z%X|
Also known as Ternary operator.
If the expression is true in above syntax then value1 is assigned to variable on the left hand side else value2 will be assigned.
Greater number is: 5
Used only for obects. This Operator checks whether a particular object belongs to a class/interface or not.