Java Tutorial Java Advanced Java References

Java - Operators



Operators are used to perform operation on a single operand or two operands. Operators in Java can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

Java Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
%ModuloReturns remainder of division operation

Example

Java Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent toExample
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
&=a &= ba = a & bMore Info
|=a |= ba = a | bMore Info
^=a ^= ba = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
>>>=a >>>= ba = a >>> b
<<=a <<= ba = a << bMore Info

Note: >>>= is just like the >>= operator, except that the bits shifted in on the left are always zero.

Java Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator Description
==Equal
!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

Java Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

OperatorNameDescriptionExample
++xPre-incrementIncreases the value of x by 1, then returns x.Example
x++Post-incrementReturns x, then increases the value of x by 1.
--xPre-decrementDecreases the value of x by 1, then returns x.Example
x--Post-decrementReturns x, then decreases the value of x by 1.

Java Logical operators

Logical operators are used to combine two or more conditions.

Operator Name Description
&&ANDReturns true when all conditions are true
||ORReturns true when any of the conditions is true
!NOTReturns opposite boolean result

More Info

Java Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
& ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0More Info
~ NOTReverse all the bitsMore Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operandMore Info
>>>Right shift with ZeroThis operator is just like the >> operator, except that the bits shifted in on the left are always zero.
<<Left shiftThe left operand value is moved left by the number of bits present in the right operandMore Info

Java Miscellaneous operators

The table below describes other operators supported by Java:

OperatorDescription
instanceof operatorReturns true if the object is an instance of a specific type (class, subclass or interface), false otherwise.
ternary operator (?:)Returns one of the two values based on value of boolean expression.

Java Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of Java operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

PrecedenceOperatorDescriptionAssociativity
16.Object member accessLeft to Right
[ ]Array element access
( )Parentheses
15a++  a--Postfix Increment, Postfix DecrementNA
14!Logical NOTRight to Left
~Bitwise NOT
+a  -aUnary plus, Unary negation
++a  --aPrefix Increment, Prefix Decrement
13( )Cast
newObject creation
12*  /  %Multiplication, Division, RemainderLeft to Right
11+  -Addition, Subtraction, string concatenation
10<<  >>  >>>Bitwise left shift, right shift and unsigned right shift
9<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
instanceofInstance of Object
8==  !=Equality, Inequality
7&Bitwise AND
6^Bitwise XOR
5|Bitwise OR
4&&Logical AND
3||Logical OR
2a?b:cConditional (ternary) operatorRight to Left
1=Direct assignment
+=  -=  *=  /=  %=Compound assignment by sum, difference, product, quotient and remainder
<<=  >>=  >>>=Compound assignment by Bitwise left shift, right shift and unsigned right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR

5