Professional Documents
Culture Documents
Java provides a rich set of operat ors t o manipulat e variables. We can divide all t he Java operat ors int o
t he following groups:
Assume int eger variable A holds 10 and variable B holds 20, t hen:
Show Examples
+ Addit ion - Adds values on eit her side of t he operat or A + B will give 30
- Subt ract ion - Subt ract s right hand operand from left hand A - B will give -10
operand
* Mult iplicat ion - Mult iplies values on eit her side of t he operat or A * B will give 200
/ Division - Divides left hand operand by right hand operand B / A will give 2
% Modulus - Divides left hand operand by right hand operand and B % A will give 0
ret urns remainder
Show Examples
< Checks if t he value of left operand is less t han t he value of right (A < B) is t rue.
operand, if yes t hen condit ion becomes t rue.
>= Checks if t he value of left operand is great er t han or equal t o (A >= B) is not
t he value of right operand, if yes t hen condit ion becomes t rue. t rue.
<= Checks if t he value of left operand is less t han or equal t o t he (A <= B) is t rue.
value of right operand, if yes t hen condit ion becomes t rue.
Bit wise operat or works on bit s and performs bit -by-bit operat ion. Assume if a = 60; and b = 13; now
in binary format t hey will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
~a = 1100 0011
Show Examples
& Binary AND Operat or copies a bit t o t he result if it exist s in bot h (A & B) will give
operands. 12 which is 0000
1100
| Binary OR Operat or copies a bit if it exist s in eit her operand. (A | B) will give 61
which is 0011
1101
^ Binary XOR Operat or copies t he bit if it is set in one operand but (A ^ B) will give
not bot h. 49 which is 0011
0001
~ Binary Ones Complement Operat or is unary and has t he effect of (~A ) will give -61
'flipping' bit s. which is 1100
0011 in 2's
complement
form due t o a
signed binary
number.
<< Binary Left Shift Operat or. The left operands value is moved left A << 2 will give
by t he number of bit s specified by t he right operan 240 which is 1111
0000
>> Binary Right Shift Operat or. The left operands value is moved A >> 2 will give
right by t he number of bit s specified by t he right operand. 15 which is 1111
>>> Shift right zero fill operat or. The left operands value is moved A >>>2 will give
right by t he number of bit s specified by t he right operand and 15 which is 0000
shift ed values are filled up wit h zeros. 1111
Assume Boolean variables A holds t rue and variable B holds false, t hen:
Show Examples
&& Called Logical AND operat or. If bot h t he operands are non-zero, (A && B) is false.
t hen t he condit ion becomes t rue.
! Called Logical NOT Operat or. Use t o reverses t he logical st at e of !(A && B) is t rue.
it s operand. If a condit ion is t rue t hen Logical NOT operat or will
make false.
Show Examples
= Simple assignment operat or, Assigns values from right side C = A + B will
operands t o left side operand assign value of A
+ B int o C
-= Subt ract AND assignment operat or, It subt ract s right operand C -= A is
from t he left operand and assign t he result t o left operand equivalent t o C =
C-A
*= Mult iply AND assignment operat or, It mult iplies right operand C *= A is
wit h t he left operand and assign t he result t o left operand equivalent t o C =
C*A
Following is t he example:
Value of b is : 30
Value of b is : 20
If t he object referred by t he variable on t he left side of t he operat or passes t he IS-A check for t he
class/int erface t ype on t he right side, t hen t he result will be t rue. Following is t he example:
true
This operat or will st ill ret urn t rue if t he object being compared is t he assignment compat ible wit h t he
t ype on t he right . Following is one more example:
class Vehicle {}
true
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operat or * has higher precedence
t han +, so it first get s mult iplied wit h 3*2 and t hen adds int o 7.
Here, operat ors wit h t he highest precedence appear at t he t op of t he t able, t hose wit h t he lowest
appear at t he bot t om. Wit hin an expression, higher precedence operat ors will be evaluat ed first .