DRAFT
Observed
Out-of-bounds array elements are null
Out-of-bounds array elements cannot be accessed
Accessing an out-of-bounds element of an array throws an ArrayIndexOutOfBoundsException
.
For example, in the following code, the while loop condition
is not a correct way to finish the loop.
a[i]
will not produce null
when i >= a.length
;
instead the expression will throw an ArrayIndexOutOfBoundsException
.
public int m(int[] a) {
int i = 0;
int sum = 0;
while (a[i] != null) { // error
sum += a[i];
i++;
}
return sum;
}
Follow us on twitter to hear about new misconceptions.