Operators in Java
Operator in Java is a symbol that is used to
perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which
are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Java Operator Precedence
Operator Category Precedence
Type
Unary postfix expr++ expr--
prefix ++expr --expr
+expr -expr ~ !
Arithmetic multiplicativ * / %
e
additive + -
Shift shift << >> >>>
Relational comparison < > <= >=
instanceof
equality == !=
Bitwise bitwise AND &
bitwise ^
exclusive OR
bitwise |
inclusive OR
Logical logical AND &&
logical OR ||
Ternary ternary ? :
Assignmen assignment = += -= *= /= %= &=
t ^= |= <<= >>= >>>=
Java Unary Operator
The Java unary operators require only one
operand. Unary operators are used to perform
various operations i.e.:
o incrementing/decrementing a value by one
o negating an expression
o inverting the value of a boolean
Java Unary Operator Example: ++ and --
1.public class OperatorExample{
2.public static void main(String args[]){
3.int x=10;
4.System.out.println(x++);//10 (11)
5.System.out.println(++x);//12
6.System.out.println(x--);//12 (11)
7.System.out.println(--x);//10
8.}}
Output:
10
12
12
10
Java Unary Operator Example 2: ++ and --
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=10;
5.System.out.println(a++ + ++a);//10+12=22
6.System.out.println(b++ + b++);//10+11=21
7.
8.}}
Output:
22
21
Java Unary Operator Example: ~ and !
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=-10;
5.boolean c=true;
6.boolean d=false;
7.System.out.println(~a);
//-11 (for any n value ~(n)=-(n+1))
8.System.out.println(~b);//9
9.System.out.println(!c);//false (opposite of boo
lean value)
10. System.out.println(!d);//true
11. }}
Output:
-11
9
false
true
Java Arithmetic Operators
Java arithmetic operators are used to perform
addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
Java Arithmetic Operator Example
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.System.out.println(a+b);//15
6.System.out.println(a-b);//5
7.System.out.println(a*b);//50
8.System.out.println(a/b);//2
9.System.out.println(a%b);//0
10. }}
Output:
15
5
50
2
0
Java Arithmetic Operator Example: Expression
1.public class OperatorExample{
2.public static void main(String args[]){
3.System.out.println(10*10/5+3-1*4/2);
4.}}
Output:
21
Types of Relational Operators in Java
Types of Relational Operators
The commonly used relational operators in Java are:
Equal to (==): Checks if two values are equal.
Not equal to (!=): Verifies if two values are not equal.
Greater than (>): Tests if the left operand is greater than
the right operand.
Less than (<): Determines if the left operand is less than
the right operand.
Greater than or equal to (>=): Check if the left operand
is greater than or equal to the right operand.
Less than or equal to (<=): Verifies if the left operand is
less than or equal to the right operand.
Java Left Shift Operator
The Java left shift operator << is used to shift all
of the bits in a value to the left side of a specified
number of times.
Java Left Shift Operator Example
1.public class OperatorExample{
2.public static void main(String args[]){
3.System.out.println(10<<2);//
10*2^2=10*4=40
4.System.out.println(10<<3);//
10*2^3=10*8=80
5.System.out.println(20<<2);//
20*2^2=20*4=80
6.System.out.println(15<<4);//
15*2^4=15*16=240
7.}}
Output:
40
80
80
240
Java Right Shift Operator
The Java right shift operator >> is used to move
the value of the left operand to right by the
number of bits specified by the right operand.
Java Right Shift Operator Example
1.public OperatorExample{
2.public static void main(String args[]){
3.System.out.println(10>>2);//10/2^2=10/4=2
4.System.out.println(20>>2);//20/2^2=20/4=5
5.System.out.println(20>>3);//20/2^3=20/8=2
6.}}
Output:
2
5
2
Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn't check the second
condition if the first condition is false. It checks
the second condition only if the first one is true.
The bitwise & operator always checks both
conditions whether first condition is true or false.
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int c=20;
6.System.out.println(a<b&&a<c);//false && tru
e = false
7.System.out.println(a<b&a<c);//false & true =
false
8.}}
Output:
false
false
Java AND Operator Example: Logical && vs Bitwise &
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int c=20;
6.System.out.println(a<b&&a++<c);//false &&
true = false
7.System.out.println(a);//10 because second co
ndition is not checked
8.System.out.println(a<b&a++<c);//false && tr
ue = false
9.System.out.println(a);//11 because second co
ndition is checked
10. }}
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.
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int c=20;
6.System.out.println(a>b||a<c);//true || true =
true
7.System.out.println(a>b|a<c);//true | true = tr
ue
8.//|| vs |
9.System.out.println(a>b||a++<c);//true || true
= true
10. System.out.println(a);//10 because secon
d condition is not checked
11. System.out.println(a>b|a++<c);//true | tr
ue = true
12. System.out.println(a);//11 because secon
d condition is checked
13. }}
Output:
true
true
true
10
true
11
Java Ternary Operator
Java Ternary operator is used as one line
replacement for if-then-else statement and used
a lot in Java programming. It is the only
conditional operator which takes three operands.
Java Ternary Operator Example
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=2;
4.int b=5;
5.int min=(a<b)?a:b;
6.System.out.println(min);
7.}}
Output:
2
Another Example:
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=5;
5.int min=(a<b)?a:b;
6.System.out.println(min);
7.}}
Output:
5
Java Assignment Operator
Java assignment operator is one of the most
common operators. It is used to assign the value
on its right to the operand on its left.
Java Assignment Operator Example
1.public class OperatorExample{
2.public static void main(String args[]){
3.int a=10;
4.int b=20;
5.a+=4;//a=a+4 (a=10+4)
6.b-=4;//b=b-4 (b=20-4)
7.System.out.println(a);
8.System.out.println(b);
9.}}
Output:
14
16
Java Assignment Operator Example
1.public class OperatorExample{
2.public static void main(String[] args){
3.int a=10;
4.a+=3;//10+3
5.System.out.println(a);
6.a-=4;//13-4
7.System.out.println(a);
8.a*=2;//9*2
9.System.out.println(a);
10. a/=2;//18/2
11. System.out.println(a);
12. }}
Output:
13
9
18
9
Java Assignment Operator Example: Adding short
1.public class OperatorExample{
2.public static void main(String args[]){
3.short a=10;
4.short b=10;
5.//a+=b;//a=a+b internally so fine
6.a=a+b;//Compile time error because 10+10=
20 now int
7.System.out.println(a);
8.}}
Output:
Compile time error
After type cast:
1.public class OperatorExample{
2.public static void main(String args[]){
3.short a=10;
4.short b=10;
5.a=(short)(a+b);//20 which is int now convert
ed to short
6.System.out.println(a);
7.}}
Output:
20