Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. For example, multiplication and division have a higher precedence than addition and subtraction. Precedence rules can be overridden by explicit parentheses.
Precedence Order
When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition.Associativity
When two operators with the same precendence the expression is evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-to-left associativty (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity.Precedence and associativity of Java operators
The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and even those that do still use parentheses for clarity.Operator | Description | Level | Associativity |
[] . () ++ -- | access array element access object member invoke a method post-increment post-decrement | 1 | left to right |
++ -- + - ! ~ | pre-increment pre-decrement unary plus unary minus logical NOT bitwise NOT | 2 | right to left |
() new | cast object creation | 3 | right to left |
* / % | multiplicative | 4 | left to right |
+ - + | additive string concatenation | 5 | left to right |
<< >> >>> | shift | 6 | left to right |
< <= > >= instanceof | relational type comparison | 7 | left to right |
== != | equality | 8 | left to right |
& | bitwise AND | 9 | left to right |
^ | bitwise XOR | 10 | left to right |
| | bitwise OR | 11 | left to right |
&& | conditional AND | 12 | left to right |
|| | conditional OR | 13 | left to right |
?: | conditional | 14 | right to left |
= += -= *= /= %= &= ^= |= <<= >>= >>>= | assignment | 15 | right to left |
Post a Comment