# Different types of operators in Java

java.Akarsh Sachan

Symbols that are used to perform a certain operation are known as operators. Examples: +,-,/,* etc.

Different types of operators available in Java are:

• Arithmetic Operator
• Assignment Operator
• Logical Operator
• Bitwise Operator
• Relational Operator
• Shift Operator
• Unary Operator, and
• Ternary Operator ## Examples to understand different operators

### Arithmetic Operator

``````class ArithmeticOperator {
public static void main(String[] args) {
int a = 15;
int b = 5;
System.out.println(a + b); //20
System.out.println(a - b); //10
System.out.println(a * b); //75
System.out.println(a / b); //3
System.out.println(a % b); //0
}
}

class ArithmeticOperatorExpr {
public static void main(String[] args) {
System.out.println(10 * 10 / 5 + 3 - 1 * 4 / 2); //21
}
}``````

### Assignment Operator

``````class AssignmentOperator {
public static void main(String[] args) {
int a = 15;
int b = 5;
a += 4; //a=a+4
b -= 4; //b=b-4
System.out.println(a); //0
System.out.println(b); //0
}
}

class AssignmentOperator {
public static void main(String[] args) {
int a = 15;
a += 4;
System.out.println(a);
a -= 4;
System.out.println(a);
a *= 2;
System.out.println(a);
a /= 2;
System.out.println(a);
}
}

class AssignmentOperator {
public static void main(String[] args) {
short a = 15;
short b = 5;
a = a + b; //Should throw Compile time error because 10+10=20 wiz int
System.out.println(a);
}
}``````

### After type casting

``````class AssignmentOperator {
public static void main(String[] args) {
short a = 15;
short b = 5;
a = (short) a + b; //20 which is now converted to short
System.out.println(a);
}
}``````

### Logical Operator

``````class LogicalOperator {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 20;
System.out.println(a < b && a++ < c); //false && true = false
System.out.println(a); //10 because second condition is checked
System.out.println(a > b || a++ < c); //true || true = true
System.out.println(a); //10 because second condition is checked
}
}``````

### Bitwise Operator

``````class BitwiseOperator {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 20;
System.out.println(a < b & a++ < c); //false & true= false
System.out.println(a); //11 because second condition is checked
System.out.println(a > b | a++ < c); //true | true = true
System.out.println(a); //12 because second condition is checked
}
}`````` ### Relational Operator

``````class RelationalOperator {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 20;
if (a >= b)
System.out.println(a);
If(b < c)
System.out.println(b);
If(a != c)
System.out.println(c);
}
}``````

### Shift Operator

• Left Shift: << operator is used to shift all bits in the value to the left, the specified number of times.
• Right Shift: >> operator is used to shift all bits in the value to the right, the specified number of times.
``````class ShiftOperator {
public static void main(String[] args) {
System.out.println(10 << 2); //10*2^2=10*4=40
System.out.println(10 << 3); //10*2^3=10*8=80
System.out.println(20 << 2); //20*2^2=20*4=80
System.out.println(15 << 4); //15*2^4=15*16=240
System.out.println(10 >> 2); //10/2^2=10/4=2
System.out.println(10 >> 3); //10/2^3=10/8=1
System.out.println(20 >> 2); //20/2^2=20/4=5
System.out.println(15 >> 4); //15/2^4=15/16=0
}
}``````

### Unary Operator

Uses:

• Incrementing/decrementing a value by one
• Negating an expression
• Inverting the value of a boolean
``````class UnaryOperator {
public static void main(String[] args) {
int a = 10;
int x = 10;
int y = -10;
boolean c = true;
Boolean d = false;
System.out.println(x++); //10(11)
System.out.println(++x); //12
System.out.println(x--); //12(11)
System.out.println(--x); //10
System.out.println(x++ + ++x); //10+12=22
System.out.println(x++ + x++); //12+13=25
System.out.println(~x); //-11 (minus of total positive value which starts from 0)
System.out.println(~y); //9 (positive of total minus, positive starts from 0)
System.out.println(!c); //false (opposite of Boolean value)
System.out.println(!d); //true
}
}``````

### Ternary Operator

``````class TernaryOperator {
public static void main(String[] args) {
int a = 2;
int b = 5;
int min = (a < b) ? a : b;
System.out.println(min);
}
}``````

If you liked this article, please upvote and recommend it to show your support. Feel free to ask any questions in the comments below.

We publish articles on web development and technology frequently. Consider subscribing to our newsletter or follow us on our social channels (twitter, Facebook, LinkedIn).

## Share and support us

Share on social media and help us reach more people