In this tutorial, we will clarify what are operators in Java. We will also focus on why operators are used in Java and also see how they are used in a Java program.

## What are Operators in Java?

Operators in Java are special symbols that have a predefined meaning in the Java compiler that perform some operation with one or more operands and then return an output (value).

There are eight different types of operators available in Java.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Shift Operators
- Unary Operators
- Ternary Operators

## What are Arithmetic Operators?

Arithmetic Operators are used in arithmetic or mathematical type of operations. These operations can be multiplication, division, addition, etc.

Operator |
Operation |
Returns |
Example |

+ | Performs addition of values on both side of the operator | Sum of values | 2 + 5 returns 7 |

– | Performs subtraction of values by subtracting the value on the right-hand side from the value on the left-hand side of the operator | Difference of values | 5 – 4 returns 1 |

* | Performs multiplication of values on both side of the operator | Product of values | 3 * 4 returns 12 |

/ | Performs division of value on the left-hand side with the value on the right-hand side of the operator | The quotient obtained by division | 10 / 2 returns 5 |

% | Performs division of value on the left-hand side with the value on the right-hand side of the operator | Remainder obtained by division | 11 / 2 returns 1 |

## What are Relational Operators?

Relational Operators are used as a condition to check whether the output is true or false.

Operator |
Operation |
Returns |
Example |

== | Verifies if the operand on the left-hand side of the operator is equal to the right-hand side of the operator | True if both the values are equal, if not then false | 36 == 36 returns True |

< | Verifies if the operand on the left-hand side is smaller than the operand on the right-hand side of the operator | Returns True if the left operand is smaller than the right operand, else return False | 53 < 24 returns False |

> | Checks if the operand on the left-hand side is greater than the operand on the right-hand side of the operator | Returns True if the left operand is bigger than the right operand, if not then returns False | 53 > 24 returns True |

<= | Checks if the operand on the left-hand side is smaller than or equal to the operand on the right-hand side of the operator | Returns True if the left operand is smaller than or equal to the right operand, else returns False | 23 <= 23 returns True |

>= | Checks if the operand on the left-hand side is greater than or equal to the operand on the right-hand side of the operator | Returns True if the left operand is greater than or equal to the right operand, else returns False | 23 >= 24 returns False |

!= | Verifies if the operand on the left-hand side of the operator is not equal to the right-hand side of the operator | Returns True if the left operand is not equal to the right operand, else returns False | 5 != 64 returns True |

## What are Logical Operators?

A Logical operator is a special symbol that takes one or two conditions (based on the operator) and returns the logical output in the form of true/false.

Operator |
Operation |
Returns |
Example |

&& | Performs logical AND operation on either side of the operator | True if both the conditions are true, if not then False | 2<4 && 3>1 returns true |

|| | Performs logical OR operation on either side of the operator | True if one or both the conditions are true, if not then False | 2<4 || 3<1 returns true |

! | Performs logical NOT operation on a single operand | Return True if the condition is False. Returns False if the condition is True | !(23>4) return False |

## What are Assignment Operators?

An Assignment Operator is basically used to simply assign a value to a variable.

Operator |
Operation |
Example |

= | Assigns the value on the right-hand side to the operand on the left-hand side of the operator | v = 23 |

+= | Assigns the sum of operands on either side to left-hand side operand | v += 43 |

-= | Assigns the difference of operands on either side to left-hand side operand | v -= 73 |

*= | Assigns the product of operands on either side to the left-hand side operand | v *= 32 |

/= | Assigns the quotient obtained by division of operands on either side of the operator to left-hand side operand | v /= 87 |

%= | Assigns the remainder obtained by division of operands on either side of the operator to left-hand side operand | v %= 12 |

^= | Performs left operand to the power of right operand and assigns its result to left operand | v ^= 3 |

## What are the Bitwise Operators?

Bitwise Operators are used to performing per bit of operations. If there are two values say 4 and 6, then the bitwise operation will be performed on their binary form.

Operator |
Operation |
Returns |
Example |

& | Performs bitwise AND operation on both the operands | Bitwise AND of operands | 4&4 returns 4 |

| | Performs bitwise OR operation on both the operands | Bitwise OR of operands | 12|9 returns 13 |

^ | Performs bitwise XOR operation on both the operands | Bitwise XOR of operands | 12|9 returns 5 |

## What are Shift Operators?

Shift Operators are used for shifting purposes. Shift Operators are used to shifting bits if a number either left to right or right to left. Shifting also causes a number to either multiply or divide based on the type of shifting.

There are two types of shifting that can be done in Java:

- Left Shift (<<)
- Right Shift (>>)

Shift Operators are represented by pairs of (less than/greater than) symbols.

## What are the Unary Operators?

Unary Operators are used to performing unary operations on a single operand. They are used to either increment a value or decrement a value by 1.

Operator |
Operation |
Returns |
Example |

++ | Used to increment a value | Returns Previous value plus 1 when used before a variable/value otherwise returns the previous value and then increments the variable/value by 1 | a++, ++a |

— | Used to decrement a value | Returns Previous value minus 1 when used before a variable/value otherwise returns a previous value and then decrements the variable/value by 1 | b–, –b |

! | Used to invert a Boolean value | Returns True if the value is False, else Return False | !c |

## What are the Ternary Operators?

A Ternary operator has one condition and two outputs. When the condition is True, the first output is returned, if not then the second output is returned.

The basic form of a Ternary operator is:

(condition) ? (Output 1) : (Output 2)

### Example:

class Program { public static void main (String agmt[]) { //Using Assignment Operator int n1 = 25; int n2 = 58; int n3 = 76; //Using Relational Operator if(n2 > n1) System.out.println("n2 is greater than n1"); //Using Arithmetic Operator n3 = n1 + n2; //Using Logical Operator if (n3>n1 && n3>n2) System.out.println("n3 is the greatest number"); } }

### Output

n2 is greater than n1

n3 is the greatest number

In the above example, we can see the variables n1, n2, and n3 are being assigned values using an Assignment Operator (=). The if-condition is using a Relational Operator (>) to compare the values of n2 and n1. Finally, we have used a Logical Operator (&&) to check if the conditions on either side are true are not.