Java Reference
In-Depth Information
The type of the array access expression is the result of applying capture conversion
5.1.10 ) to T .
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 ab-
ruptly, 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 NullPointerEx-
ception 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 expres-
sion a is fully evaluated before the expression (a=b)[3] ; this means that the ori-
ginal 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:
Click here to view code image
class Test1 {
Search WWH ::




Custom Search