Java Reference
In-Depth Information
public static void main(String[] args) {
int[] a = { 11, 12, 13, 14 };
int[] b = { 0, 1, 2, 3 };
System.out.println(a[(a=b)[3]]);
}
}
prints:
14
because the monstrous expression's value is equivalent to
a[b[3]]
or
a[3]
or
14
.
Example 15.13-2. Abrupt Completion of Array Reference Evaluation
If evaluation of the expression to the left of the brackets completes abruptly,
no part of the expression within the brackets will appear to have been evalu-
ated. Thus, the program:
class Test2 {
public static void main(String[] args) {
int index = 1;
try {
skedaddle()[index=2]++;
} catch (Exception e) {
System.out.println(e + ", index=" + index);
}
}
static int[] skedaddle() throws Exception {
throw new Exception("Ciao");
}
}
prints:
java.lang.Exception: Ciao, index=1
because the embedded assignment of
2
to
index
never occurs.
Example 15.13-3.
null
Array Reference
If the array reference expression produces
null
instead of a reference to an ar-
ray, then a
NullPointerException
is thrown at run time, but only after all parts of