##### 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 Name | Operator | Associativity |
---|---|---|

parentheses Access array element Access Object member | () [] . | Left to Right |

unary | expr++, expr-- | Not associative |

unary | ++expr, --expr, !, ~, +(unary), -(unary) | Right to Left |

cast object creation | () new | Right to Left |

Multiplicative | *, /, % | Left to Right |

Additive String Concatenation | +, - + | Left to Right |

Shift | <<, >>, >>> | Left to Right |

Relational | >, >=, <, <=, instanceof | Not 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.

### Overloading +

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

1 2 3 4 5 6 7 8 |
// Demonstration for overloaded plus public class Overloaded_plus { public static void main(String[] args) { int X=1, Y=2, Z=3; System.out.println("A String + X + Y + Z = " + X + Y + Z); System.out.println("A String + (X + Y + Z) = " + (X + Y + Z)); } } |

The output is :

1 2 |
A String + X + Y + Z = 123 A String + (X + Y + Z) = 6 |

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

1 2 3 4 5 6 7 8 9 10 11 12 |
// Demonstration of postfix and prefix operators public class Postfix_vs_Prefix { public static void main(String[] args) { int W, X, Y, Z; W = X = Y = Z = 7; System.out.println("W = X = Y = Z = " + W); System.out.println("W++ + W++ = " + (W++ + W++)); // 7 + 8 System.out.println("X++ + ++X = " + (X++ + ++X)); // 7 + 9 System.out.println("++Y + Y++ = " + (++Y + Y++)); // 9 + 7 and not 8 + 8 System.out.println("++Z + ++Z = " + (++Z + ++Z)); // 8 + 9 } } |

The output is :

1 2 3 4 5 |
W = X = Y = Z = 7 W++ + W++ = 15 X++ + ++X = 16 ++Y + Y++ = 16 ++Z + ++Z = 17 |

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

1 2 3 4 5 6 7 8 9 |
// Demonstration for precedence of Ternary operator public class Ternary_operator { public static void main(String[] args){ int X = 2; int Y = -5; int Z = X>0 && Y>0 ? 1:-1; System.out.println(Z); } } |

The output is :

1 |
-1 |

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

1 2 3 4 5 6 7 8 9 10 11 |
// Demonstration for parentheses and cast public class Parentheses_1 { public static void main(String[] args) { double X=3.8; double Y, Z; Y = (int)X + 0.5; Z = (int)(X + 0.5); System.out.println("(int)X + 0.5 = " + Y); System.out.println("(int)(X + 0.5) = " + Z); } } |

The output is

1 2 |
(int)X + 0.5 = 3.5 (int)(X + 0.5) = 4.0 |

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 :

1 2 3 4 5 6 7 8 9 10 11 |
// Demonstration for parentheses and cast public class Parentheses_2 { public static void main(String[] args) { double X=3.8; double Y, Z; Y = (int)(X + 0.8)*2; Z = (int)( (X + 0.8)*2 ); System.out.println("(int)(X + 0.8)*2 = " + Y); System.out.println("(int)( (X + 0.8)*2 ) = " + Z); } } |

The output is :

1 2 |
(int)(X + 0.8)*2 = 8.0 (int)( (X + 0.8)*2 ) = 9.0 |

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