refresh java logo

Precedence of Operators in Java

Operator precedence is a rule that decides which operator will be executed first in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example in an expression a+b*c, the operator * will be evaluated before + operator, which means operator * has higher precedence than + operator. This is what operator precedence rule in java tell us.

Table bellow shows the precedence of operators in decreasing order, the operators appearing first in table have higher precedence than operators appearing later. Operators appearing in same row of table have equal precedence. When operators of equal precedence appears in the same expression, the operator in left will be evaluated first except for assignment(=) operator which means all binary operators except for the assignment operator are evaluated from left to right while assignment operator is evaluated right to left. Consider a and b as two operands for the examples given in below table.

Operators Precedence Example
Postfix expr++ expr-- a++ , a--
Unary ++expr --expr +expr -expr ~ ! ++a , --a , !a
Multiplicative * / % a*b , a/b , a%b
Additive + - a+b , a-b
Shift << >> >>> a<<2 , a>>1
Relational < > <= >= instanceof a<2 , a>1
Equality == != a==b , a!=b
Bitwise AND & a&b
Bitwise exclusive OR ^ a^b
Bitwise inclusive OR | a|b
Logical AND && a&&b
Logical OR || a||b
Ternary ? : a = a>2 ? a : b
Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= a=b, a+=b, a/=b, a>>=2

A programmer should remember bellow rules while evaluating an expression in java.

  • The operands of operators are evaluated from left to right.
  • Every operand of an operator (except the conditional operators &&, ||, and ? :) are evaluated completely before any part of the operation itself is performed.
  • The left-hand operand of a binary operator are evaluated completely before any part of the right-hand operand is evaluated.
  • Order of evaluation given by parenthesis () get's preference over operator precedence.
  • The prefix version of ++ or -- evaluates the incremented value while postfix version of ++ or -- evaluates the current value, then increments/decrements by one.
  • All binary operators are evaluated from left to right except assignment operator.

How I can execute my low precedence operator before high precedence operator ?

Use the expression having low precedence operator inside a parenthesis (). For example in an expression a*(b+c), + will be evaluated first.

Java Program of Operator Precedence

 class OperatorPrecedence
  {
     public static void main (String[] args) 
      {
         int result = 0;
         result = 5 + 2 * 3 - 1;
         System.out.println("5 + 2 * 3 - 1 = " +result);
					
         result = 5 + 4 / 2 + 6;
         System.out.println("5 + 4 / 2 + 6 = " +result);
					
         result = 3 + 6 / 2 * 3 - 1 + 2;
         System.out.println("3 + 6 / 2 * 3 - 1 + 2 = " +result);
					
         result = 6 / 2 * 3 * 2 / 3;
         System.out.println("6 / 2 * 3 * 2 / 3 = " +result);
					 
         int x = 2;
         result = x++ + x++ * --x / x++ - --x + 3 >> 1 | 2;
         System.out.println("result = " +result);				
      }
  }

Output:

5 + 2 * 3 - 1 = 10
5 + 4 / 2 + 6 = 13
3 + 6 / 2 * 3 - 1 + 2 = 13
6 / 2 * 3 * 2 / 3 = 6
result = 2

Let's understand step by step how the last expression x++ + x++ * --x / x++ - --x + 3 >> 1 | 2 is evaluated.

  operands are evaluated first, left to right	 
  2 + 3 * 3 / 3 - 3 + 3 >> 1 | 2
  * and / have higher preference than other, * evaluated first as it comes in left
  2 + 9 / 3 - 3 + 3 >> 1 | 2
  / have higher preference than others
  2 + 3 - 3 + 3 >> 1 | 2
  + and - have higher preference than others
  5 >> 1 | 2
  >> have higher preference than | 
  2 | 2
  Output 
  2 
★★★
  • Nested parentheses in an expression are evaluated from the innermost parentheses to the outermost parenthesis.
  • If evaluation of the left-hand operand of a binary operator does not complete properly, no part of the right-hand operand will be evaluated.
  • Arguments in a method are evaluated from left to right.