If I have the following code:
array[index] = someValue;
does the someValue
or the index
get evaluated first?
If I have the following code:
array[index] = someValue;
does the someValue
or the index
get evaluated first?
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
It looks like the index
expression is evaluated first.
This test program confirms it:
public class Main {
public static void main(String[] args) {
int[] array = new int[10];
array[getIndex()] = getValue();
}
private static int getIndex() {
System.out.println("getIndex!");
return 0;
}
private static int getValue() {
System.out.println("getValue!");
return 1;
}
}
Output:
getIndex!
getValue!
The value is evaluated even if an ArrayIndexOutOfBoundsException
is thrown. Changing getIndex()
:
private static int getIndex() {
System.out.println("getIndex!");
return -1; // Hey!
}
Output:
getIndex!
getValue!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Main.main(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)