15.12.2
Examples: Array Access Evaluation Order
EXPRESSIONS
15.12.2 Examples: Array Access Evaluation Order
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 evalu
ated. 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.
342
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved