Because you're confusing operator associativity with order of evaluation.
The = operator is right-associative, so the expression is equivalent toPeople have the tendency to evaluate expressions by performing operations in parentheses first. That's not how Java works. Java always evaluates left-to-right, meaning the i in iA[i] is evaluated to 0 before i = 30 is evaluated, and finally 30 is assigned to iA.
Regarding associativity: "When an expression has two operators with the same precedence, 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 associativity (and an assignment statement evaluates to the value on the right hand side)."
Order of evaluation of subexpressions: "Associativity and precedence determine in which order Java applies operators to subexpressions but they do not determine in which order the subexpressions are evaluated. In Java, subexpressions are evaluated from left to right..."
which confused me even more...
Stephan van Hulst
posted 3 weeks ago
Precedence and associativity tell the compiler which operands go with which operators. You can kind of compare it to the compiler inserting explicit parentheses in the code, as the example x = (y = (z = 17)) demonstrates. This happens during compilation.
At runtime, when the expression is evaluated, Java has no idea about precedence and associativity. It just sees a tree of expressions that consist of sub-expressions. Those sub-expressions are evaluated left-to-right.
The easiest way to do this yourself is to first write down the complete expression and inserting parentheses everywhere until you can tell which operand goes with which operator without having to think about precedence rules. Then repeatedly scan the expression from left to right until you find a complete sub-expression. Evaluate it, and then rescan the resulting expression.
In the example given by the website, the first complete sub-expression is (z = 17). In the example you gave at the start, (iA[i]) is the first complete sub-expression.