Java Operators

Operators perform operations on operands like variables or values.

In Java, there are five types of operators based on the type of operations they perform. They are

  1. Java Arithmetic Operators
  2. Java Assignment Operators
  3. Java Relational Operators
  4. Java Logical Operators
  5. Java Bitwise Operators

1. Arithmetic Operators

Java Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in Java.

Arithmetic OperationOperator SymbolExampleDescription
Addition + a + bReturns sum of a and b.
Subtraction - a – bReturns difference of b from a.
Multiplication * a * bReturns product of a and b.
Division / a / bReturns the quotient when a is divided by b.
Modular Division % a % bReturns reminder when a is divided by b.
Increment ++ a++ is Post-Increment.
++a is Pre-Increment.
Increments the value of a by one.
Post-Increment: The value of a is updated, only after the execution moves to next statement.
Pre-Increment: The value of a is updated during the execution of this statement itself.
Decrement -- a-- is Post-Decrement.
--a is Pre-Decrement.
Decrements the value of a by one.
Post-Decrement: The value of a is updated, only after the execution moves to next statement.
Pre-Decrement: The value of a is updated during the execution of this statement itself.

Arithmetic Operators Tutorials

The following tutorials provide a well detailed explanation and examples for each of the Arithmetic Operators in Java.

  1. Java Addition Operator
  2. Java Subtraction Operator
  3. Java Multiplication Operator
  4. Java Division Operator
  5. Java Modulus Operator
  6. Java Increment Operator
  7. Java Decrement Operator

2. Assignment Operators

Java Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).

The following table specifies symbol, example, and description for each of the Assignment Operator in Java.

Assignment OperationOperator SymbolExampleDescription
Simple Assignment=x = 2Assign x with 2.
Addition Assignment+=x += 3Add 3 to the value of x and assign the result to x.
Subtraction Assignment-=x -= 3Subtract 3 from x and assign the result to x.
Multiplication Assignment*=x *= 3Multiply x with 3 and assign the result to x.
Division Assignment/=x /= 3Divide x with 3 and assign the quotient to x.
Remainder Assignment%=x %= 3Divide x with 3 and assign the remainder to x.
Bitwise AND Assignment&=x &= 3Perform x & 3 and assign the result to x.
Bitwise OR Assignment|=x |= 3Perform x | 3 and assign the result to x.
Bitwise-exclusive-OR Assignment^=x ^= 3Perform x ^ 3 and assign the result to x.
Left-shift Assignment<<=x <<= 3Left-shift the value of x by 3 places and assign the result to x.
Right-shift Assignment>>=x >>= 3Right-shift the value of x by 3 places and assign the result to x.

Assignment Operators Tutorials

The following tutorials provide a well detailed explanation and examples for each of the Assignment Operators in Java.

  1. Java Simple Assignment (=) Operator
  2. Java Addition Assignment Operator
  3. Java Subtraction Assignment Operator
  4. Java Multiplication Assignment Operator
  5. Java Division Assignment Operator
  6. Java Remainder Assignment Operator
  7. Java Bitwise AND Assignment Operator
  8. Java Bitwise OR Assignment Operator
  9. Java Bitwise XOR Assignment Operator
  10. Java Left-shift Assignment Operator
  11. Java Right-shift Assignment Operator

3. Relational Operators

Relational Operators are used to compare two operands if they are equal, greater, lesser, etc.

The following table gives more information about these relational operators in java.

Relational OperationOperator SymbolExampleDescription
Equal to==a == bReturns true if a is equal to b, else false.
Not equal to!=a != bReturns true if a is not equal to b, else false.
Greater than>a > bReturns true if a is greater than b, else false.
Less than<a < bReturns true if a is less than b, else false.
Greater than or equal to>=a >= bReturns true if a is greater than or equal to b, else false.
Less than or equal to<=a <= bReturns true if a is less than or equal to b, else false.

Relational Operators Tutorials

The following tutorials provide a well detailed explanation and examples for each of the Relational Operators in Java.

  1. Java Equal To Operator
  2. Java Not Equal Operator
  3. Java Greater Than Operator
  4. Java Less Than Operator
  5. Java Greater Than or Equal To Operator
  6. Java Less Than or Equal To Operator

4. Logical Operators

Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.

Java supports the following Logical Operators.

Logical OperationOperator SymbolExampleDescription
AND&&a && bReturns logical AND of a and b.
OR||a || bReturns logical OR of a and b.
NOT!!aReturns logical Not of a.

Logical Operators Tutorials

The following Java tutorials cover each of these Logical Operators in detail.

  1. Java AND Operator
  2. Java OR Operator
  3. Java NOT Operator

5. Bitwise Operators

Java Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.

The following table specifies symbol, example, and description for each of the Assignment Operator in Java.

Bitwise OperationOperator SymbolExampleDescription
AND&x & yReturns the bitwise AND operation between x and y.
OR|x | yReturns the bitwise OR operation between x and y.
XOR^x ^ yReturns the bitwise XOR operation between x and y.
Complement~~xReturns the complement of x.
Left Shift<<x << yReturns the result of x left shifted by y number of places.
Right Shift>>x >> yReturns the result of x right shifted by y number of places.

Bitwise Operators Tutorials

The following Java tutorials cover each of these Bitwise Operators in detail.

  1. Java Bitwise AND Operator
  2. Java Bitwise OR Operator
  3. Java Bitwise XOR Operator
  4. Java Bitwise Complement Operator
  5. Java Bitwise Left-shift Operator
  6. Java Bitwise Right-shift Operator

Conclusion

In this Java Tutorial, we learned about different operators in Java, with separate tutorial for each of the operator.