10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.10.3 Example: Array Creation and Out-of-Memory Detection EXPRESSIONS<br />

434<br />

prints:<br />

[4,3]<br />

because the first dimension is calculated as 4 before the second dimension expression<br />

sets i to 3.<br />

If evaluation of a dimension expression completes abruptly, no part of any<br />

dimension expression to its right will appear to have been evaluated. Thus, the<br />

example:<br />

class Test {<br />

public static void main(String[] args) {<br />

int[][] a = { { 00, 01 }, { 10, 11 } };<br />

int i = 99;<br />

try {<br />

a[val()][i = 1]++;<br />

} catch (Exception e) {<br />

System.out.println(e + ", i=" + i);<br />

}<br />

}<br />

static int val() throws Exception {<br />

throw new Exception("unimplemented");<br />

}<br />

}<br />

prints:<br />

java.lang.Exception: unimplemented, i=99<br />

because the embedded assignment that sets i to 1 is never executed.<br />

15.10.3 Example: Array Creation and Out-of-Memory Detection<br />

If evaluation of an array creation expression finds there is insufficient memory to<br />

perform the creation operation, then an OutOfMemoryError is thrown. This check<br />

occurs only after evaluation of all dimension expressions has completed normally.<br />

So, for example, the test program:<br />

class Test {<br />

public static void main(String[] args) {<br />

int len = 0, oldlen = 0;<br />

Object[] a = new Object[0];<br />

try {<br />

for (;;) {<br />

++len;<br />

Object[] temp = new Object[oldlen = len];<br />

temp[0] = a;<br />

a = temp;<br />

}<br />

} catch (Error e) {<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!