Is the array index or the assigned value evaluated first?

arrays, java

Solution

The index is evaluated first. See JLS section 15.26.1, in particular:

15.26.1. Simple Assignment Operator =

...

If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:

First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.

Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.

Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

TL;DR: The order is 1[2]=3

Problem

If I have the following code: ``` array[index] = someValue; ``` does the `someValue` or the `index` get evaluated first?

Original source

Related problems