Operator Precedence in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

There exist many arithmetics and logical operators in Java like +, -, /, *, &, |, etc that can be applied in an expression. Operator precedence in java is a set of rules which tells the compiler the order in which the operators given in a particular expression are evaluated if there are multiple operators in a single expression.

Java Operator Precedence

During our primary schooling, we learned about the BODMAS rule which helps us to determine which operator to be solved first if many operators are there in a given equation.

Similarly, we have the operator precedence in java which is a set of rules that tells us the precedence of different operators, and as per the rule operator with higher precedence is evaluated first. For example in the expression x * y - z the * operator will be evaluated before the - operator, because the * operator has higher precedence than the - operator.

The precedence of each operator has been clearly defined which is also shown in the table given later in the article.

Note that it is always possible to modify the result of an expression by placing parentheses in the original expression. For example - 20 / 2 * 5 will evaluate to 50 but if we want to get 2 as result we can modify the expression to 20 / (2 * 5).

Operator Associativity - So we have seen that the operator with higher precedence is evaluated first. But if two operators in an expression are with the same precedence then the operator and operand are grouped according to their associativity.

The associativity of each operator has also been clearly defined in the table given in the next section. For example - 50 / 2 / 5 will be treated as (50 / 2) / 2 because the / operator is left-to-right associative.

Java Operator Precedence Table

The following table well defines the precedence of operators in java. Here the operators present at top of the table have higher precedence while those who are placed lower in the table have lower precedence.

OperatorDescriptionPrecedenceAssociativity
()
[]
.
Parentheses
Array Subscript
Member Function
16Left to Right
++
--
Unary Post Increment
Unary Post Decrement
15Right to Left
++
--
+
-
!
~
Unary Pre Increment
Unary Pre Decrement
Unary Addition
Unary Subtraction
Unary Logical Negation
Unary Bitwise Complement
14Right to Left
()
new
Cast
Object Creation
13Right to Left
*
/
%
Multiplication
Division
Modulus
12Left to Right
+
-
Addition
Subtraction
11Left to Right
>>
<<
Bitwise Right Shift
Bitwise Left Shift
10Left to Right
<
<=
>
>=
instanceof
Less than
Less than or equal
Greater than
Greater than or equal
Type Comparison
9Left to Right
==
!=
Equal to
Not equal to
8Left to right
&Bitwise AND7Left to Right
^Bitwise XOR6Left to Right
|Bitwise OR5Left to Right
&&Logical AND4Left to Right
||Logical OR3Left to Right
?:Ternary Conditional2Right to Left
=
+=
-=
*=
/=
%=
Assignment
Addition Assignment
Subtraction Assignment
Multiplication Assignment
Division Assignment
Modulus Assignment
1Right to Left

Examples of Operator Precedence in Java

Example 1

Output

The precedence of * and / is greater than that of + operator hence they will be evaluated. Also, * and / are of the same precedence so they will be associated in left to right order. Therefore -
ans=x+yz/10ans = x + y * z / 10

is equivalent to ans=x+((yz)/10)ans = x + ((y * z) / 10)

Example 2

Output

The precedence of ++ and -- is greater than that of -, and + operators. Hence, ans=x+y++zans = x + --y - ++z

is equivalent to ans=x+(y)(++b)ans = x + (--y) - (++b)

Example 3

Output

The precedence of ++ and -- is greater than that of + and << operators. So Hence, they will be evaluated first. Also, the precedence of << is greater than that of + operator. Therefore -

ans=++x+++y)+5<<12ans = ++x + ++y) + 5 << 1 | 2

is equivalent to ans=(((++x)+(++y)+5)<<1)2ans = (((++x) + (++y) + 5) << 1)| 2

which will be solved like -

  • ans=((3+4+5)<<1)2ans = ((3 + 4 + 5) << 1) | 2
  • ans=(12<<1)2ans = (12 << 1) | 2
  • ans=242ans = 24 | 2
  • ans=26ans = 26

Practice Questions

Question 1

Evaluate the following expression - 20+12152/2/520 + 12 - 15 * 2 / 2 / 5

Answer - 29

Explanation
Since the precedence of * and / is greater than that of + and - operators.

20+12152/2/520 + 12 - 15 * 2 / 2 / 5

is equivalent to

20+12(((152)/2)/5)20 + 12 - (((15 * 2) / 2) / 5)

32((30/2)/5)32 - ((30 / 2) / 5)

32(15/5)32 - (15 / 5)

32332 - 3

2929

Question 2

Evaluate the following expression -

108+1254/1010 | 8 + 12 - 5 * 4 / 10

Answer - 26

Explanation
Since the precedence of * and / is greater than that of |, + and - **operators.

108+1254/1010 | 8 + 12 - 5 * 4 / 10

is equivalent to

10(8+12((54)/10))10 | (8 + 12 - ((5 * 4) / 10))

10(8+12(20/10))10 | (8 + 12 - (20 / 10))

10(8+12(20/10))10 | (8 + 12 - (20 / 10))

10(8+122)10 | (8 + 12 - 2)

101810 | 18.

2626

Question 3

Evaluate the following expression -

10+((1520)(2/2))/510 + ((15 - 20) * (2 / 2)) / 5

Answer - 9

Explanation
Since parentheses have the highest precedence among all the operators, expressions written inside them will be evaluated first.

10+((1520)(2/2))/510 + ((15 - 20) * (2 / 2)) / 5.

10+(51)/510 + (-5 * 1) / 5

10+5/510 + -5 / 5

10+(1)10 + (-1)

99

Question 4

Predict the output of the following code

Output

Explanation
The precedence of the assignment operator = is the least, and equal to the operator (==) is left to right-associative.

x==y==zx == y == z

is equivalent to

(x==y)==z(x == y) == z
(false==true)==false(false == true) == false
false==falsefalse == false
true

Question 5

Predict the output of the following code

Output

Explanation
The above code results in a compile-time error because Java parses the statement as ++(++x) and the term ++x increases the value by 1 and evaluates it to 6 now we will have ++6 which is illegal because the pre-increment operator in java can only be applied to a variable, not to a value.

Conclusion

  • Operator precedence in java determines the order in which the operators given in a particular expression are evaluated if there are multiple operators in a single expression.
  • Operators with higher precedence are evaluated before the operator(s) with lower precedence.
  • If two operators have the same level of precedence then the tie is broken by their associativity.