4. Operators in Java
Operator is a symbol which is used to perform operations on
one or more operands to produce a result.
⚫Arithmetic Operator,
⚫Relational Operator,
⚫Bitwise Operator,
⚫Logical Operator,
⚫Unary operator,
⚫Ternary Operator
⚫Assignment Operator
*
Java Programming Skills
4
8. Bitwise Shift Operators
• The left shift operator << is used to shift all of the
bits in a value to the left side of a specified number
of times. Left shift by 1 is equivalent to a
multiplication by 2.
• The right shift operators >>(signed) and >>>
(unsigned) are used to move left operands value to
right by the number of bits specified by the right
operand. Right shift by 1 is equivalent to a division
by 2.
*
Java Programming Skills
8
11. Bitwise Operators
⚫Bitwise operator performs bit by bit processing.
⚫num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */
⚫num1 & num2 compares corresponding bits of
num1 and num2 and generates 1 if both bits are
equal, else it returns 0. In our case it would return: 2
which is 00000010 because in the binary form of
num1 and num2 only second last bits are matching.
⚫num1 | num2 compares corresponding bits of num1
and num2 and generates 1 if either bit is 1, else it
returns 0. In our case it would return 31 which is
00011111 *
Java Programming Skills
11
12. Bitwise Operators
⚫num1 ^ num2 compares corresponding bits of
num1 and num2 and generates 1 if they are not
equal, else it returns 0. In our example it would
return 29 which is equivalent to 00011101
⚫~num1 is a complement operator that just
changes the bit from 0 to 1 and 1 to 0. In our
example it would return -12 which is signed 8 bit
equivalent to 11110100
*
Java Programming Skills
12
13. Bitwise operators
public class BitwiseOp
{ public static void main(String args[])
{ int num1 = 11; // 11 = 00001011
int num2 = 22; //22 = 00010110
System.out.println(num1 & num2); //2
System.out.println(num1 | num2); //31
System.out.println(num1 ^ num2); //29
System.out.println(~num1); //-12
}
}
*
Java Programming Skills
13
14. Ternary operator
⚫This is used as one liner replacement for if-else
statement. This is the only conditional operator which
takes three operands.
class Main{
public static void main(String args[])
{
int a=12, b=15;
int min=(a<b)?a:b;
System.out.println(min); //12
}
}
*
Java Programming Skills
14
15. Relational / Comparison Operators
public class Main
{
public static void main(String[] args)
{ int a=10,b=10;
System.out.println(a==b); //true
System.out.println(a!=b); //false
System.out.println(a>b); //false
System.out.println(a<b); //false
System.out.println(a>=b); //true
System.out.println(a<=b); //true
}
}
*
Java Programming Skills
15
17. Assignment operator
⚫It is used to assign the value on its right to the
operand on its left.
class Main{
public static void main(String args[]){
int a=10, b=20;
a+=4; //a=a+4(a=10+4)
b-=4; //b=b-4(b=20-4)
System.out.println(a); //14
System.out.println(b); //16
}}
*
Java Programming Skills
17
18. instanceof
⚫instanceof operator is used to test whether the object
is an instance of the specified type (class or subclass or
interface).
⚫The instanceof in java is also known as
type comparison operator because it compares the
instance with type. It returns either true or false. If we
apply the instanceof operator with any variable that has
null value, it returns false.
*
Java Programming Skills
18
19. Instanceof operator example1
public class Main
{
public static void main (String[]args)
{
Main s=new Main();
Main s1=null;
System.out.println(s instanceof Main);//true
System.out.println(s1 instanceof Main);//false
System.out.println(s); //address of s
System.out.println(s1); //address of s1 is null
}
}
*
Java Programming Skills
19
20. Instanceof operator example2
class Main
{
public static void main (String[] args)
{
String name = "Programiz";
Integer age = 22;
int a=22;
System.out.println(name instanceof String)); //true
System.out.println((Integer)a instanceof Integer)); //true
}
}
*
Java Programming Skills
20
21. Control Statements
⚫control statements are used to control the flow of
execution of a program.
⚫Java control statements can be put into the following
three categories: selection, iteration, and jump.
⮚The selection statements allow program to choose a
different path of execution based on a condition.
⮚Iteration statements allow program execution to
repeat one or more statements.
⮚Jump statements transfer the control to other parts of
the program.
*
Java Programming Skills
21
23. Assignment Questions
1. Explain in detail about the different types of
operators available in java.
2. Write a java program to implement assignment,
unary, relational, and ternary operators?
3. Explain in detail about the bitwise operators with an
suitable program?
*
23 Java Programming Skills
24. Quiz-3 Google form URL
⚫https://forms.gle/BcgQF8XgjJD3GvFV8
*
Java Programming Skills
24