Previous | Home | Next |
When two or more operator in an expression then which will be evaluated first, operator work in 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.
When an expression has two operators with the same precedence, the expression is evaluated according to its associativity.
Example :
5+4*2*5%-8+13%-5 5+8*5%-8+13%-5 5+40%-8+13%-5 5+0+3
Output :
8
Precedence of Operator
Operator | Description | Level | Associativity |
[] | access array element | ||
. | access object member | ||
() | invoke a method | 1 | Left to Right |
++ | post-increment | ||
-- | post-decrement | ||
++ | pre-increment | ||
-- | pre-decrement | ||
+ | unary plus | 2 | Right to Left |
- | unary minus | ||
! | logical NOT | ||
~ | bitwise NOT | ||
() | cast | ||
new | object creation | 3 | Right to Left |
* | |||
/ | multiplicative | 4 | Left to Right |
% | |||
+- | additive | ||
+ | string concatenation | 5 | Left to Right |
<<>> | shift | 6 | Left to Right |
>>> | |||
< <= | |||
> >= | relational type comparison | 7 | Left to Right |
instanceof | |||
== | 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 |
&= ^= |= | conditional AND | 12 | Left to Right |
<<= >>= >>>= | |||
Previous | Home | Next |