Java Reference
In-Depth Information
for (int i = 0; i < triang.length; i++)
triang[i] = new float[i+1];
If evaluation of an array creation expression finds there is insufficient memory to perform
the creation operation, then an OutOfMemoryError is thrown. If the array creation expression
does not have an array initializer, then this check occurs only after evaluation of all di-
mension expressions has completed normally. If the array creation expression does have
an array initializer, then an OutOfMemoryError can occur when an object of reference type is
allocated during evaluation of a variable initializer expression, or when space is allocated
for an array to hold the values of a (possibly nested) array initializer.
Example 15.10.1-3. OutOfMemoryError and Dimension Expression Evaluation
Click here to view code image
class Test3 {
public static void main(String[] args) {
int len = 0, oldlen = 0;
Object[] a = new Object[0];
try {
for (;;) {
++len;
Object[] temp = new Object[oldlen = len];
temp[0] = a;
a = temp;
}
} catch (Error e) {
System.out.println(e + ", " + (oldlen==len));
}
}
}
This program produces the output:
java.lang.OutOfMemoryError, true
because the out-of-memory condition is detected after the dimension expression oldlen
= len is evaluated.
Compare this to class instance creation expressions (§ 15.9 ) , which detect the out-of-
memory condition before evaluating argument expressions (§ 15.9.4 ) .
Search WWH ::




Custom Search