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

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.

ADVERTISEMENT

Java 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.

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.

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.

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.

Conclusion

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