Operators
Operators
By
Ravi Kant Sahu
Asst. Professor, LPU
Outlines [Expected Time: 1 Hours]
• Introduction
• Assignment Operator
• Arithmetic Operator
• Relational Operator
• Bitwise Operator
• Conditional Operator
• Type Comparison Operator
• Unary Operator
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Assignment Operator
One of the most common operators is the simple
assignment operator "=".
This operator assigns the value on its right to the
operand on its left.
Example:
int salary = 25000; double speed = 20.5;
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Compound Assignments
Arithmetic operators are combined with the simple
assignment operator to create compound assignments.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Relational Operators
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Relational Operators
Operator Description
== equal to
!= not equal to
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class Parent{}
class Child extends Parent{}
class TestInstanceOf {
public static void main(String [] rk){
Parent p = new Parent();
Child c = new Child();
System.out.println(p instanceof Child);
System.out.println(p instanceof Parent);
System.out.println(c instanceof Parent); }
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Unary Operators
The unary operators require only one operand.
Operator Description
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Short-Circuit Logical Operators
• These are secondary versions of the Boolean AND and OR
operators, and are known as short-circuit logical operators.
• If we use the || and && forms, rather than the | and & forms
of these operators, Java will not bother to evaluate the right-
hand operand when the outcome of the expression can be
determined by the left operand alone.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
This is very useful when the right-hand operand depends
on the value of the left one in order to function properly.
For example
if (denom != 0 && num / denom > 10)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The ? Operator
• Java includes a special ternary (three-way)operator, ?, that can
replace certain types of if-then-else statements.
• The ? has this general form:
expression1 ? expression2 : expression3
• Here,expression1 can be any expression that evaluates to a
boolean value.
• If denom does not equal zero, then the expression after the colon
is evaluated and used for the value of the entire ? expression.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bitwise Logical Operators
• The bitwise logical operators are
• ~ (NOT)
• & (AND)
• | (OR)
• ^ (XOR)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Left Shift Operator
• The left shift operator,<<, shifts all of the bits in a value to
the left a specified number of times.
value << num
• Example:
01000001 65 << 2
00000100 4
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Right Shift Operator
• The right shift operator, >>, shifts all of the bits in a value
to the right a specified number of times.
value >> num
• It is also known as signed right shift.
• Example:
00100011 35 >> 2
00001000 8
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• When we are shifting right, the top (leftmost) bits exposed
by the right shift are filled in with the previous contents of
the top bit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Unsigned Right Shift
• In these cases, to shift a zero into the high-order bit no matter
what its initial value was. This is known as an unsigned shift.
• Example:
– 11111111 11111111 11111111 11111111 –1 in
binary as an int
– >>>24
– 00000000 00000000 00000000 11111111 255 in
binary as an int
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Operator Precedence
Highest
() [] .
++ -- %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= Op=
Lowest
Brainstorming 1
What will be the output of the following code snippets?
System.out.println(34>>3);
System.out.println(-34>>3);
System.out.println(-34>>>3);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 2
int x = 10, y = 5;
while(x- - > 7 || + + y < 8 );
System.out.print(x);
System.out.print(y);
A. 95
B. 67
C. 78
D. 48
E. 77
F. N.O.T
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 3
System.out.print(2>1||4>3?false:true);
class X{}
class Y extends X{}
class Z extends Y{}
X x1 = new Y();
Y y1 = new Z();
Y y2 = new Y();
System.out.println( x1 instanceof X);
System.out.println( x1 instanceof Z);
System.out.println( y1 instanceof Z);
System.out.println( y2 instanceof X);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 4
System.out.print(2>1||4>3?false:true);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)