| CHAPTER 15: Expressions |
Previous |
Java Language |
Index |
Next |
An array access expression refers to a variable that is a component of an array.
ArrayAccess: ExpressionName [ Expression ] PrimaryNoNewArray [ Expression ]
An array access expression contains two subexpressions, the array reference expression (before the left bracket) and the index expression (within the brackets). Note that the array reference expression may be a name or any primary expression that is not an array creation expression (S15.9).
The type of the array reference expression must be an array type (call it T[] , an array whose components are of type T) or a compile-time error results. Then the type of the array access expression is T.
The index expression undergoes unary numeric promotion (S5.6.1); the promoted type must be int .
The result of an array reference is a variable of type T, namely the variable within the array selected by the value of the index expression. This resulting variable, which is a component of the array, is never considered final , even if the array reference was obtained from a final variable.
An array access expression is evaluated using the following procedure:
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. For example, in the (admittedly monstrous) expression a[(a=b)[3]] , the expression a is fully evaluated before the expression (a=b)[3] ; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a .
Thus, the example:
class Test {
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 .
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 evaluated. Thus, the example:
class Test {
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.
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at run time, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally. Thus, the example:
class Test {
public static void main(String[] args) {
int index = 1;
try {
nada()[index=2]++;
} catch (Exception e) {
System.out.println(e + ", index=" + index);
}
}
static int[] nada() { return null; }
}
prints:
java.lang.NullPointerException, index=2
because the embedded assignment of 2 to index occurs before the check for a null pointer. As a related example, the program:
class Test {
public static void main(String[] args) {
int[] a = null;
try {
int i = a[vamoose()];
System.out.println(i);
} catch (Exception e) {
System.out.println(e);
}
}
static int vamoose() throws Exception {
throw new Exception("Twenty-three skidoo!");
}
}
always prints:
java.lang.Exception: Twenty-three skidoo!
A NullPointerException
never occurs, because the index expression must be completely evaluated before any part of the indexing operation occurs, and that includes the check as to whether the value of the left-hand operand is null
.
| © 1996 Sun Microsystems, Inc. All rights reserved. |