Operators¶
Operators are functions that act upon elements to create new elements. There are 5 types of operator groups in Java.
Arithmetic
Assignment
Bitwise
Comparison
Logical
Arithmetic¶
Arithmetic operators are common mathematical operators.
Operator |
Meaning |
Example |
Result |
---|---|---|---|
+ |
Addition |
10 + 5 |
15 |
- |
Subtraction |
50 - 8 |
42 |
* |
Multiplication |
4 * 6 |
24 |
/ |
Division |
2.0 / 1.0 |
1.0 |
++ |
Increment by 1 |
z++ |
z + 1 |
– |
Decrement by 1 |
z– |
z - 1 |
% |
Modulo |
5 % 2 |
1 |
Assignment¶
Assignment operators are used to store values in defined variables.
Operator |
Meaning |
Example |
Example expanded |
---|---|---|---|
= |
Assign |
x = 10 |
x = 10 |
*= |
Multiply the current value by |
x *= 5 |
x = x * 5 |
/= |
Divide the current value by |
x /= 5 |
x = x / 5 |
%= |
Mudulo the current value by |
x %= 5 |
x = x % 5 |
+= |
Add to the current value by |
x += 5 |
x = x + 5 |
-= |
Subtract the current value by |
x -= 5 |
x = x - 5 |
<<= |
Shift the current value left by |
x <<= 8 |
x = x << 8 |
>>= |
Shift the current value right by |
x >>= 8 |
x = x >> 8 |
&= |
Bitwise AND the current value by |
x &= 0xFF |
x = x & 0xFF |
^= |
Bitwise XOR the current value by |
x ^= 0xFF |
x = x ^ 0xFF |
|= |
Bitwise OR the current value by |
x |= 0xFF |
x = x | 0xFF |
Bitwise¶
Bitwise operators manipulate the individual bits of numbers.
X = 10 and Y = 2
Operator |
Meaning |
Example |
Result |
---|---|---|---|
~ |
Unary NOT |
~X |
-11 |
& |
AND |
X & Y |
2 |
| |
OR |
X | Y |
10 |
^ |
XOR |
X ^ Y |
8 |
>> |
Shift Right |
X >> 1 |
5 |
<< |
Shift Left |
X << 1 |
20 |
>>> |
Shift right zero fill |
X >>> 1 |
5 |