Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators.
Precedence Order
When two operators share an operand the operator with the higher precedence goes first.
Associativity
When two operators with the same precendence the expression is evaluated according to its associativity.
for example :
consider : 2 + 6 * 7 /4
since * and / have same precedence and have L to R associativity, * is evaluated first then /.
so it is evaluated as :
2 + ((6 * 7) / 4)
|
Operator Name |
Operators |
Associative |
| access array element, access object member, invoke a method |
[] . () | L to R |
| unary | ++ – - + - ~ ! (<data_type) | R to L |
| creation or cast | new (type)expression | R to L |
| multiplicative | * / % | L to R |
| additive | + - | L to R |
| shift | << , >> , >>> | L to R |
| relational | < , > , <= , >= , instance of | L to R |
| equality | == , != | L to R |
| bitwise AND | & | L to R |
| bitwise XOR | ^ | L to R |
| bitwise OR | | | L to R |
| logical AND | && | L to R |
| logical OR | || | L to R |
| conditional | <boolean_expr> ? <expr1> : <expr2> | R to L |
| assignment | = *= /= %= += -= <<=>>= >>>= &= ^= | = | R to L |
Important :
post-increment or post-decrement operators have higher precedence than pre-increment operator or pre-decrement operator.




Recent Comments