01_5_Operators
01_5_Operators
Arithmetic Operators
Unary Operators
Assignment Operator
Relational Operators
Logical Operators
Ternary Operator
Bitwise Operators
Shift Operators
Arithmetic Operators: simple arithmetic operations on primitive data types.
: Multiplication
/ : Division
% : Modulo
+ : Addition
- Substraction
Unary Operators: Unary operators need only one operand. (int a=20;)
increment, decrement or negate a value.
! Logical not operator, used for inverting a boolean value.
eg :
boolean jobDone=true;
boolean flag=!jobDone;
System.out.println(flag);
Assignment Operator : ‘=’ assign a value to any variable. It has a right to left
associativity.
eg : int a=200;
In many cases assignment operator can be combined with other operators to build a
shorter version of statement called Compound Statement.
int a=100;
a += 10;
System.out.println(a);
+=, for adding left operand with right operand and then assigning it to variable on the
left.
-=, for subtracting left operand with right operand and then assigning it to variable on
the left.
*=, for multiplying left operand with right operand and then assigning it to variable on
the left.
/=, for dividing left operand with right operand and then assigning it to variable on the
left.
%=, for assigning modulo of left operand with right operand and then assigning it to
variable on the left.
Relational Operators : These operators are used to check for relations like equality,
greater than, less than. They return boolean result after the comparison and are used
in looping statements and conditional if else statements.
==, Equal to : returns true if left hand side is equal to right hand side.
!=, Not Equal to : returns true if left hand side is not equal to right hand side.
<, less than : returns true if left hand side is less than right hand side.
<=, less than or equal to : returns true if left hand side is less than or equal to right
hand side.
>, Greater than : returns true if left hand side is greater than right hand side.
>=, Greater than or equal to: returns true if left hand side is greater than or equal to
right hand side
Logical Operators :
&&, Logical AND
||, Logical OR
Ternary operator :
condition ? if true : if false
Bitwise Operators :
&, Bitwise AND operator.
|, Bitwise OR operator
^, Bitwise XOR operator
~, Bitwise Complement Operator:
Output:
-11
9
false
true
Output:
21
Left Shift
Output:
40
80
80
240
Right Shift
Output:
2
5
2
Output:
5
5
-5
1073741819
Java AND Operator Example: Logical && and Bitwise &
Output:
false
false
Output:
false
10
false
11
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is true.
It checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or
false.
Output:
true
true
true
10
true
11
Output:
Another Example:
Output:
Output:
14
16
Java Assignment Operator Example
Output:
13
9
18
9
Adding short
Output:
20
Operator Shifting
1)Bitwise Left Shift Operator (<<)
x=10, x<<2
10 = 00001010.
x=10, x>>2
10 is 00001010.
When we apply >>> on a positive number, it gives the same output as that of >>.
It gives a positive number when we apply >>> on a negative number. MSB is
replaced by a 0.
Observe the above example, after shifting the bits to the right the binary
number 00100000 (in decimal 32) becomes 00000100 (in decimal 4). The last three
bits shifted out and lost.
Example
Let's see the left and right shifting through example:
Output: