Operators in Java
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:
In Java, negative numbers are stored as 2's complement.
Java Unary Operator Example: ++ and --
Java Unary Operator Example 2: ++ and --
Java Unary Operator : ~ and !
Output:
-11
9
false
true
Java Arithmetic Operator Example
Java Arithmetic Operator Example: Expression
Output:
21
Left Shift
Output:
40
80
80
240
Right Shift
Output:
2
5
2
Java Shift Operator Example: >> vs >>>
Output:
5
5
-5
1073741819
Java AND Operator Example: Logical && and Bitwise &
Output:
false
false
Java AND Operator Example: Logical && vs Bitwise &
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
Java Ternary Operator Example
Output:
Another Example:
Output:
Java Assignment Operator Example
1. public class OperatorExample{
Output:
14
16
Java Assignment Operator Example
Output:
13
9
18
9
Adding short
Output:
Compile time error
After type cast:
Output:
20
Operator Shifting
1)Bitwise Left Shift Operator (<<)
x=10, x<<2
10 = 00001010.
00101000 (in decimal 40).
2)Bitwise Right Shift Operator
>> shifts the bits towards the right and also preserve the sign bit, which is the
leftmost bit. The leftmost bit represents the sign of the number. The sign
bit 0 represents a positive number, and 1 represents a negative number. So after
performing >> on a positive number, we get a positive value in the result also.
When we perform >> on a negative number, again we get a negative value.
x=10, x>>2
10 is 00001010.
00000010 (in decimal 2).
Bitwise Zero Fill Right Shift Operator (>>>)
Bitwise Zero Fill Right Shift Operator shifts the bits of the number towards
the right a specified n number of positions. The sign bit filled with 0's. The symbol
>>> represents the Bitwise Zero Fill Right Shift Operator.
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.
Difference between >> and >>> operator
Both >> and >>> are used to shift the bits towards the right. The difference is that
the >> preserve the sign bit while the operator >>> does not preserve the sign bit.
To preserve the sign bit, you need to add 0 in the MSB.
Example
Let's see the left and right shifting through example:
1. public class OperatorShifting
2. {
3. public static void main(String args[])
4. {
5. byte x, y;
6. x=10;
7. y=-10;
8. System.out.println("Bitwise Left Shift: x<<2 = "+(x<<2));
9. System.out.println("Bitwise Right Shift: x>>2 = "+(x>>2));
10.System.out.println("Bitwise Zero Fill Right Shift: x>>>2 = "+(x>>>2));
11. System.out.println("Bitwise Zero Fill Right Shift: y>>>2 = "+(y>>>2));
12.}
13. }
Output:
Bitwise Left Shift: x<<2 = 40
Bitwise Right Shift: x>>2 = 2
Bitwise Zero Fill Right Shift: x>>>2 = 2
Bitwise Zero Fill Right Shift: y>>>2 = 1073741821