Which Java operator is right associative
Answer : Below operator are right associative:
= += -=
*= /= %=
&= ^= |=
<<= >>= >>>=
++
—
+
–
!
~
Below is full list of operator associativity:
Associativity | Description | Level | Operator |
left to right | access array elementaccess object memberinvoke a methodpost-incrementpost-decrement | 1 | [].()++– |
right to left | pre-incrementpre-decrementunary plusunary minuslogical NOTbitwise NOT | 2 | ++–+-!~ |
right to left | castobject creation | 3 | ()new |
left to right | multiplicative | 4 | */% |
left to right | additivestring concatenation | 5 | + -+ |
left to right | shift | 6 | << >>>>> |
left to right | relationaltype comparison | 7 | < <=> >=instanceof |
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 | = += -=*= /= %=&= ^= |=<<= >>= >>>= |
Please see example below:
public class OperatorAssociativeTest { public static void main(String[] args) { int i = 19 - 20 + 21; // (+- are left associative so it will be // (19-20)+21=20 System.out.println(i); } }
Output: