## Introduction to Java Operators

Java Operator denotes a symbol that helps in performing several operations on one or more than one operand. Operators can be +, -, /, *, etc. depending on the requirement. There are different types of operators. They are :

- Arithmetic Operators
- Bitwise Operators
- Assignment Operators
- Ternary Operator
- Auto-increment & Auto-decrement Operators
- Comparison operators or Relational Operators
- Logical Operators
- Shift Operators

In this document, different types of operators will be discussed in detail.

### Types of Operators in Java

Here we will discuss all the types of Java operators:

#### 1. Arithmetic Operators

Arithmetic Operators are used to perform several arithmetic operations. The following are the arithmetic operators in Java.

Operator Name/ Symbol |
Definition |
Example |

Addition(+) |
Helps in adding two values. | A+B |

Subtraction(-) |
Helps in subtracting two values. | A-B |

Modulus(%) |
Helps in getting the remainder obtained by dividing two values. | A%B |

Multiplication( * ) |
Helps in multiplying two values. | A*B |

Division ( / ) |
Helps in dividing two values. | A/B |

#### 2. Bitwise Operators

Bitwise operators are commonly used in performing bit shift and bitwise operations in Java. Below are the Bitwise operators that are commonly used.

Operator Name/ Symbol |
Definition |
Example |

Bitwise AND operator (&) |
Helps in comparing 2 operand’s corresponding bits. 1 will be returned if both the bits are 1 else 0 will be returned. | A&B |

Bitwise OR operator (|) |
Helps in comparing 2 operand’s corresponding bits. 1 will be returned if one of the bit is 1 else 0 will be returned. | A|B |

Bitwise XOR operator (^) |
Helps in comparing 2 operand’s corresponding bits. 1 will be returned if the corresponding bits are dissimilar, else 0 will be returned. | A^B |

Bitwise Complement operator (~) |
Helps in inverting the pattern of bits. That is, 1 will be changed to 0 and 0 will be changed to 1. | ~B |

#### 3. Assignment Operators

Assignment Operators are used for assigning values to certain variables. The following are the assignment operators in Java.

4.8 (4,778 ratings)

View Course

Operator Name/ Symbol |
Definition |
Example |

-= |
Helps in subtracting the right and left operators and thereby assigning the obtained result to the operator in the left. | A-=B |

/= |
Helps in dividing the right and left operators and thereby assigning the obtained result to the operator in the left. | A/=B |

*= |
Helps in multiplying right and left operators and thereby assigning the obtained result to the operator in the left. | A*=B |

+= |
Helps in adding right and left operators and thereby assigning the obtained result to the operator in the left. | A+=B |

^= |
Left operand value will be raised to the right operator power | A^=B |

%= | Modulus operator will be applied | A%=B |

#### 4. Ternary Operators

In Java, the Ternary operator is mainly used for if-then-else condition replacement. It is a one-line statement that is widely used in Java programming which takes only 3 operands.

Operator Name/ Symbol |
Definition |
Example |

condition?v1: v2 |
V1 will be returned if the condition mentioned is true and v2 will be returned if the condition is false. | A>B?A:B |

#### 5. Auto Increment Operators and Auto Decrement Operators

Auto Increment and Auto Decrement operators in Java helps in incrementing and decrementing the values by 1 respectively.

Operator Name/ Symbol |
Definition |
Example |

++ |
The value will be incremented by 1 | A++; It is similar to A+=1 |

— |
The value will be decremented by 1 | A–; It is similar to A-=1 |

#### 6. Comparison Operators or Relational Operators

Relational operators are operators that helps in checking the operand’s equality. In addition to that, these operators are also used for comparing 2 or more values.

Operator Name/ Symbol |
Definition |
Example |

equals to(==) |
If the left operand is equal to right operand, true will be returned else, false. | A==B |

not equal to(!=) |
If the left operand is not equal to the right operand, true will be returned else, false. | A!=B |

less than(<) |
If the left operand is less than the right operand, true will be returned else, false. | A<B |

greater than(>) |
If the left operand is greater than the right operand, true will be returned else, false. | A>B |

greater than or equal to(>=) |
If the left operand is greater than or equal to right operand, true will be returned else, false. | A>=B |

Less than or equal to(<=) |
If the left operand is less than or equal to right operand, true will be returned else, false. | A<=B |

#### 7. Logical Operators

Logical Operators in Java are commonly used in Boolean expressions. Below are the logical operators in Java.

Operator Name/ Symbol |
Definition |
Example |

Conditional AND(&&) |
If both the Boolean expressions are satisfied, true will be returned else, false. | A<B && B>D |

Conditional OR(||) |
If any of the Boolean expression is satisfied, true will be returned else, false. | A<B || B>D |

Logical NOT(!) |
Helps in inverting the operand’s logical state. That is, 1 will be changed to 0 and 0 will be changed to 1. | !B |

#### 8. Shift Operators

Shift operators in Java are used to shift the bits left or right based on the requirement. The following are the Shift operators.

Operator Name/ Symbol |
Definition |
Example |

Left Shift Operator ( << ) |
X in binary notation will be shifted y bits left by shifting the zeroes from the right. | A<<2 |

Right Shift Operator ( >> ) |
X in binary notation will be shifted y bits right by discarding shifted bits. | B>>2 |

Unsigned Right Shift Operator ( >>> ) |
X in binary notation will be shifted y bits right by discarding shifted bits and shifting the zeroes from the right. | A>>>2 |

Till now, we have seen different operators in Java. Now, let us see how the precedence of these operators will be.

Operator |
Precedence |

Postfix Operators | Exp++ Exp– |

Unary Operators | ++Exp –Exp +_Exp –Exp~ ! |

Multiplicative Operators | * / % |

Additive Operators | + – |

Shift Operators | << >> >>> |

Relational Operators | < > <= >= instanceof |

Equality Operators | == != |

Bitwise AND Operator | & |

Bitwise exclusive OR Operator | ^ |

Bitwise inclusive OR Operator | | |

Logical AND Operator | && |

Logical OR Operator | || |

Ternary Operators | ?: |

Assignment Operators | = += -= *= /= %= &= ^= |= <<= >>= >>>= |

### Recommended Articles

This is a guide to Java Operators. Here we discuss the Introduction and the different types of operators in Java with examples. You may also look at the following articles to learn more –