**Operators in Java** are one of the most important topics in Java Programming Language. They are used to perform various types of operations on __Variables__ and their values.

Table of Contents

[hide]- Types of Java Operators

- Arithmetic Operator in Java

- Relational Operator in Java

- Logical Operator in Java

- Ternary Operator in Java

- Assignment Operator in Java

- Bitwise Operator in Java

- Shift Operator in Java

- Unary Operator in Java

We all have been learning the basic Operators since our childhood, in school days we all have used the four basic mathematical operators i.e addition, substraction, multiplication and division. Java Operators are also used for the same purpose for performing certain operations that give us the desired output. There are eight types of operators in Java :

- 1. Arithmetic
- 2. Relational
- 3. Logical
- 4. Ternary
- 5. Assignment
- 6. Bitwise
- 7. Shift
- 8. Unary

These operators are used in performing mathematical operations on the values. It accepts numeric values as operands and perform Operations on them giving the desired single numeric value.

Operator | Short Description | Examples |

Addition ( + ) | Add the values of two operands | X + Y = 20 |

Subtraction ( - ) | Substract the values of two operands | X - Y = 10 |

Multiplication ( * ) | Multiply the values of two operands | X * Y = 40 |

Division ( / ) | Multiply the values of two operands | X / Y = 20 |

Modulus ( % ) | Divide the values of two operands and returns remainder | X % Y = 2 |

```
// Java program to illustrate Arithmetic operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 10;
int Y = 30;
System.out.println("Addition of X and Y is : " + (X + Y));
System.out.println("Substraction of X and Y is : " + (X - Y));
System.out.println("Multiplication of X and Y is : " + (X * Y));
System.out.println("Division of X and Y is : " + (X / Y));
System.out.println("Modulus of X and Y is : " + (X % Y));
}
}
```

## OUTPUT:

Addition of X and Y is : 40Substraction of X and Y is : -20Multiplication of X and Y is : 300Division of X and Y is : 0Modulus of X and Y is : 10

Relational operator in java is used to compare the equality of the operands or the values. These are mainly used in __Java Control Statements__ which we will learn about in later modules.

Operator | Short Description | Examples |

Equal to ( == ) | if values of either side of the operands are equal the condition becomes true. | (X == Y) is false |

Not Equal to ( != ) | if values of either side of the operands are not equal the condition becomes true. | (X != Y) is true |

Greater than ( > ) | if value of left operand is greater than the value of right the condition becomes true. | (X > Y) is false |

Less than ( < ) | if value of left operand is less than the value of right the condition becomes true. | (X < Y) is true |

Greater than or equal to ( >= ) | if value of left operand is greater than or equal to the value of right the condition becomes true. | (X >= Y) is false |

Less than or equal to ( <= ) | if value of left operand is lessthan or equal to the value of right the condition becomes true. | (X <= Y) is true |

```
// Java program to illustrate Relational operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 10;
int Y = 30;
System.out.println("X is equal to Y : " + (X == Y));
System.out.println("X is not equal to Y : " + (X != Y));
System.out.println("X is greater than Y : " + (X > Y));
System.out.println("X is less than Y : " + (X < Y));
System.out.println("X is greater than equal to Y : " + (X >= Y));
System.out.println("X is less than equal to Y : " + (X <= Y));
}
}
```

## OUTPUT:

X is equal to Y : falseX is not equal to Y : trueX is greater than Y : falseX is less than Y : trueX is greater than equal to Y : falseX is less than equal to Y : true

Logical Operators are often used with loops and conditional statements to determine the logic between operands or values. These operators are similar to the AND gate and OR gate in digital electronic. If the first condition is false, it does not evaluate the second consition.

Operator | Short Description | Examples |

Logical AND ( && ) | If both conditions are true on either side,it returns true | x<20&&x<30 |

Logical OR ( || ) | If any condition is true on either side,it returns true | x<20||x<5 |

Logical NOT ( ! ) | returns true if the operand produces false result | ! (x<10&&x<5) |

```
// Java program to illustrate Logical operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 10;
int Y = 10;
System.out.println("X && Y :" + ( X<7 && Y>5));
System.out.println("X || Y :" + ( X<5 || Y>5));
System.out.println("!(X && Y ) :" + !( X<5 && Y>5));
}
}
```

## OUTPUT:

X && Y :falseX || Y :true!(X && Y ) :true

It is also known as conditional operator, it works with three operands . We can use ternary operator as an alternative to __if else__ statements which we will learn in later modules.

variable result = (expression) ? (Statement1) :(Statement2)

If the output of expression is **true**, then **first statement** will be executed before the **colon (:)** else if expression evaluates to **false** then **second statement** will be executed. Let us see this with example.

```
// Java program to illustrate ternary operator in Java
public class TernaryExample{
public static void main(String args[]){
int value =20;
int result = (value == 20) ? value : 40; // Statement1 will get executed
int resultsecond = (value == 25) ? value : 40; // Statement2 will get executed
System.out.println("result is :" + result);
System.out.println("resultsecond is :" + resultsecond);
}
}
```

## OUTPUT:

result is :20resultsecond is :40

To assign values to a variable assignment operator is used, the right side operand value is assigned to the left side operand. They are mostly used with Arithmetic Operators. The result of Arithmetic operation is assigned to the variable with the help of assignment operator.

Operator | Short Description | Examples | Also Used As |

+= | Used to add the operand on both the sides and thus assigning result to the left operand | x += y | x = x + y |

+= | Used to subtract the right operand with left and thus assigning result to the left operand | x -= y | x = x - y |

*= | Used to multiply operand on both the sides and thus assigning result to the left operand | x *= y | x = x * y |

/= | Used to divide left operand with right and thus assigning result to the left operand | x /= y | x = x / y |

%= | Used to calculate modulus on both sides and thus assigning result to the left operand | x %= y | x = x % y |

^= | Used to raise left operand value to the power of right operator. | x ^= y | x = x ^ y |

```
// Java program to illustrate Assignment operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 10;
int Y = 20;
System.out.println("Y += X is : " + (Y += X));
System.out.println("Y -= X is : " + (Y -= X));
System.out.println("Y *= X is : " + (Y *= X));
System.out.println("Y /= X is : " + (Y /= X));
System.out.println("Y %= X is : " + (Y %= X));
System.out.println("Y ^= X is : " + (Y ^= X));
}
}
```

## OUTPUT:

Y += X is : 30Y -= X is : 20Y *= X is : 200Y /= X is : 20Y %= X is : 0Y ^= X is : 10

Bitwise operator in java is used for performing bitwise manipulation i.e bit by bit. It converts the numeric values into the binary format and then performs the operations and get the desired output.

Operator | Short Description | Examples |

Bitwise AND ( & ) | performs bit by bit AND operation | a & b |

Bitwise OR ( | ) | performs bit by bit OR operation | a | b |

Bitwise XOR ( ^ ) | performs bit by bit XOR operation | a ^ b |

Bitwise Compliment ( ~ ) | returns the reversed bits of the operation | ~a |

a | b | a & b | a | b | a ^ b |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

## 25 = 1 1 0 0 1

## 15 = 0 1 1 1 1

## Now if we perform bitwise operations on them, the result would be as follows :

## AND Bitwise Operator

## 1 1 0 0 1

## 1 1 0 0 1 &

## 0 1 0 0 1 = 9 in decimal

## OR Bitwise Operator

## 1 1 0 0 1

## 1 1 0 0 1 |

## 1 1 1 1 1 = 31 in decimal

## XOR Bitwise Operator

## 1 1 0 0 1

## 1 1 0 0 1 ^

## 1 0 1 1 0 = 22 in decimal

```
// Java program to illustrate Bitwise operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 25;
int Y = 15;
System.out.println("X & Y is : " + (X & Y));
System.out.println("X | Y is : " + (X | Y));
System.out.println("X ^ Y is : " + (X ^ Y));
}
}
```

## OUTPUT:

X & Y is : 9X | Y is : 31X ^ Y is : 22

Java Shift Operator is used to manipulate bits by shifting it to the respective side of the operands as specified, i.e. the left-shift operator or the right-shift operator.

Operator | Short Description | Examples |

left shift ( << ) | Add zeros according to the shift value,to the right side of the bits | a << b |

right shift ( << ) | shift the bits of the number to the right according to the shit value. | a >> b |

```
// Java program to illustrate Shift operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 8; // 1 0 0 0
int Y = X << 2; // 1 0 0 0 0 0
int Z = X >> 2; // 1 0
System.out.println("value of left shift is : " + Y);
System.out.println("value of right shift is : " + Z);
}
}
```

## OUTPUT:

value of left shift is : 32value of right shift is : 2

Unary operator in java requires one argument and is used to increment/decrement/negate or invert a value.

Operator | Short Description | Examples |

increment ( ++ ) | use to increment the given value by 1 | x ++ and ++ x |

decrement ( - - ) | use to decrement the given value by 1 | x -- and -- x |

decrement ( ! ) | use to invert the boolean value | ! x |

```
// Java program to illustrate Unary operators in Java
public class JavaOperatorExample{
public static void main(String args[]){
int X = 20;
boolean Y = true;
System.out.println("value of X++ is : " + X++); //20 -> 21
System.out.println("value of ++X is : " + ++X); //22
System.out.println("value of X-- is : " + X--); //22 -> 21
System.out.println("value of --X is : " + --X); //20
System.out.println("value of !Y is : " + !Y); //false
}
}
```

## OUTPUT:

value of X++ is : 20value of ++X is : 22value of X-- is : 22value of --X is : 20value of !Y is : false