Array Access Expressions

15.13 Array Access Expressions

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 (§15.10).

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 occurs.

The index expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int , or a compile-time error occurs.

The type of the array access expression is the result of applying capture conversion (§5.1.10) to T .

EXPRESSIONS Run-time Evaluation of Array Access 15.13.1

The result of an array access expression 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 expression denoted a final variable.

15.13.1 Run-time Evaluation of Array Access

An array access expression is evaluated using the following procedure: • First, the array reference expression is evaluated. If this evaluation completes

abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.

• Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

• Otherwise, if the value of the array reference expression is null , then a NullPointerException is thrown.

• Otherwise, the value of the array reference expression indeed refers to an array. If the value of the index expression is less than zero, or greater than or equal to the array's length , then an ArrayIndexOutOfBoundsException is thrown.

• Otherwise, the result of the array access is the variable of type T , within the array, selected by the value of the index expression.

Example 15.13-1. Array Reference Is Evaluated First

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 program: class Test1 {

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:

15.13.1 Run-time Evaluation of Array Access EXPRESSIONS

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 evaluated. 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 array, then a NullPointerException is thrown at run time, but only after all parts of the array access expression have been evaluated and only if these evaluations completed normally. Thus, the program:

class Test3 { 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:

EXPRESSIONS Postfix Expressions 15.14

java.lang.NullPointerException, index=2 because the embedded assignment of 2 to index occurs before the check for a null array

reference expression. As a related example, the program: class Test4 {

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 array access occurs, and that includes the check as to whether the value of the array reference expression is null .