Java Operators
Operators are used to perform certain operations on variables and values. The value is called an operand, while the operation ( to be performed between the two operands ) is defined by the operator.
Example:
30 + 20 = 50
Here, 30 and 20 are operand, ‘+’ is an operator and ‘=’ is an assignment operator.
The following are the types of Java operators:
Arithmetic Operators
It is used to perform various mathematical operations:
Operator | Name | Description | Example |
+ | Addition | Adds two values | a+b |
- | Subtraction | Subtracts two values | a-b |
* | Multiplication | Multiplies two values | a*b |
/ | Division | Divides two values | a/b |
% | Modulus | Returns the division remainder | a%b |
++ | Increment | Increases the value by 1 | ++a |
-- | Decrement | Decreases the value by 1 | --a |
Example
int a = 10; int b = 6; System.out.println(a+b); //Addition System.out.println(a-b); //Subtraction System.out.println(10*6); //Multiplication System.out.println(10/6); //Division System.out.println(10%6); //Modulus System.out.println(++a); //Incrementing a System.out.println(--b); //Decrementing b
Output
16 4 60 1 4 11 5
Assignment Operators
It is used to assign values to variables. = is a assignment operator.
Example
int a = 10;
There are other such assignment operators:
Operator | Example | Same as |
= | a = 4 | a = 4 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 3 | a = a * 3 |
/= | a /= 2 | a = a / 2 |
%= | a %= 10 | a = a % 10 |
&= | a &= 3 | a = a & 3 |
|= | a |= 3 | a = a | 3 |
^= | a ^= 3 | a = a ^ 3 |
>>= | a >>= 2 | a = a >> 2 |
<<= | a <<= 2 | a = a << 2 |
Comparison Operators
These operators are used to compare two values.
Operator |
Name |
Example |
== |
Equal to |
a == b |
!= |
Not equal to |
a != b |
> |
Greater than |
a > b |
< |
Less than |
a < b |
>= |
Greater than or equal to |
a >= b |
<= |
Less than or equal to |
a <= b |
Logical Operators
This operator is used to determine the logic between the variables and values.
Operator |
Name |
Description |
Example |
&& |
Logical AND |
Returns true if both statements are true |
a > 10 && a < 20 |
|| |
Logical OR |
Returns true if one of the statements are true |
a > 3 || a < 9 |
! |
Logical NOT |
Reverse the result, returns true if the result is false |
! (a > 10 && a < 20) |
Bitwise Operators
Bitwise operators works on bits and performs the bit-by-bit operation. Let see each and every bitwise operator with examples.
Example:
a = 60 , b = 12 a -> 0011 1100 b -> 0000 1100
Operator |
Description |
Example |
& (bitwise AND) |
Returns 1 if both the bits are 1 |
a & b = 12 which is 0000 1100 |
| (bitwise OR) |
Returns 1 if any one of the bit is 1 |
a | b = 60 which is 0011 1100 |
^ (bitwise XOR) |
Returns 1 only if any one of the bit is 1 |
a ^ b = 48 which is 0011 0000 |
~(bitwise compliment) |
It is a unary operator has the effects of ‘flipping’ bits |
~a = -61 which is 1100 0011 due to signed binary number |
<< (left shift) |
The left operands value are moved to left by the number of bits
specified by the right operand. |
a << 2 = 240 which is 1111 0000 |
>> (right shift) |
The right operands value are moved to right by the number of bits
specified by the right operand |
a >> 2 =15 which is 0000 1111 |