# Operator Precedence in Java

##### Precedence of Operators

Sometimes, a number of operators are used simultaneously in an expression. For example in X = 9 – 3 * 2. In such cases, the result depends on which operator has the higher precedence i.e which operator should be used first for the calculation.

In general, when two operators share an operand then the operator with higher precedence is used first. So X = 9 – 3 * 2 is evaluated as if the expression was X = 9 – (3 * 2) because multiplication has higher precedence than subtraction.

##### Associativity of Operators

There are some operators which have same precedence. For example in Y = 2 – 5 + 3, both + and – operators have same precedence. In such cases, the result depends on how the operations are carried out:  left to right, like in Y = ((2 -5) + 3) or right to left, like in Y = (2 -(5 + 3)).  It turns out that +, and – have left to right associativity hence the answer is Y=0 and not -6 but there are some operators which have right to left associativity.

### Table on precedence and associativity of Operators

The table below summarizes the precedence and associativity of operators:

• As you move down the table the precedence decreases.
• Operators in one row of the table have same precedence.

Also note that, if you have just started with Java then you may see a couple of operators for the first time here. Don’t worry, you will learn about each of them as you progress.

Operator NameOperatorAssociativity
parentheses
Access array element
Access Object member
()
[]
.
Left to Right
unaryexpr++, expr--Not associative
unary
++expr, --expr,
!, ~,
+(unary), -(unary)
Right to Left
cast
object creation
()
new
Right to Left
Multiplicative*, /, %Left to Right
String Concatenation
+, -
+
Left to Right
Shift<<, >>, >>>Left to Right
Relational>, >=, <, <=, instanceofNot associative
Equality ==, !=Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Ternary? : Right to Left
Assignment =,
*=, /=, +=, -=, %=,
>>=, <<=, >>>=,
&=, |=, ^=
Right to Left

A few points to note here are:

• All unary, cast, new, Ternary and Assignments have right to left associativity, rest have left to right associativity.
• Relational operators and some unary operators are not associative at all. Writing X>Y<Z is invalid. Suppose X, Y, Z are numeric values, and the operaton goes like (X>Y)<Z . The result of comparison X>Y is a boolean which will be compared to a integer value, and it won’t make any sense in Java. Boolean values can only be compared for equality(==, !=) and not order. Same problem arises if the operation goes like X>(Y<Z). X++– is invalid as well. However writing ~X++ is valid.
• All forms of compound assignment operators have lowest precedence.

+ is overloaded to work with both numeric and string data type.

The output is :

String concatenation (+) and numeric addition(+) operators have same precedence but their associativity is from left to right. Remember that a string + int = string. So the first print statement is evaluated as : (((A string + integer) + integer) + integer) while the other one is evaluated as : (A string + ( (integer + integer) + integer) )

### Postfix vs Prefix

The output is :

A few things to note here are:

•  Associativity of assignment(=) is from right to left, so the expression W = X = Y = Z = 7 is valid. The expression is evaluated as W = (X = (Y = (Z = 7))). Z = 7 assigns 7 to Z and returns the assigned value(7) which is assigned to Y and so on.
• In W++ + W++. The left increment operator increases the value of W to 8 but returns 7 there(nature of postfix ++). The right increment operator increases the value of W from 8 to 9 but returns 8 there. So the expression evaluates to 7 + 8 .
• In X++ + ++X. The left increment operator increases the value of X to 8 but returns 7 there(nature of postfix ++). The right increment operator increases the value of W from 8 to 9 and returns 9 there(nature of prefix ++). So the expression evaluates to 7 + 9 .
• In ++Y + Y++. The postfix increment goes first because it has the highest precedence among all three operators. It returns 7 (nature of postfix ++) then the left increment increases the value of Y to 9 from 8. So the expression evaluates to 9 + 7 and not 8 + 8 as some may think.

### An example on ternary operator

The output is :

The code returns 1 if both X and Y are positive numbers else it returns -1. The operation is carried out as if the expression was written like  Z = ( ( (X>0) && (Y>0) ) ? 1 : -1) . It outlines that out of =, >, &&, and ? : , the order of application of the operators is >, &&, ? : , then = . It happens so because out of the four operators > lies highest in the table among them and = lies lowest in the table among them.

### Use of parentheses

The output is

See how the two results are different.  In (int)X + 0.5, first the cast operator is applied then 0.5 is added, while in (int)(X + 0.5) we overrule the precedence of cast operator over + by using parentheses.  You can even nest the parentheses like in :

The output is :

In general, even the redundant parentheses don’t degrade the performance of your program. So use them to avoid ambiguity whenever required.