A component of an array is accessed by an array access expression (§15.12) that
consists of an expression whose value is an array reference followed by an indexing expression enclosed by [
and ]
, as in A[i]
. All arrays are 0
-origin. An array
with length n can be indexed by the integers 0
to n-1
.
Arrays must be indexed by int
values; short
, byte
, or char
values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int
values. An attempt to access an array component with a long
index value results in a compile-time error.
All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an IndexOutOfBoundsException
to be thrown.