Java Array Type

Posted by yangyuan on May 16, 2017

from Java Language Specification 8

Array elements

The element type of an array may be any type, whether primitive or reference. In particular:

• Arrays with an interface type as the element type are allowed. An element of such an array may have as its value a null reference or an instance of any type that implements the interface.

• Arrays with an abstract class type as the element type are allowed. An element of such an array may have as its value a null reference or an instance of any subclass of the abstract class that is not itself abstract.

An array’s length is not part of its type.

Array index

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.

Array members

The members of an array type are all of the following:

• The public final field length, which contains the number of components of the array. length may be positive or zero.

• The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

• All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

Array class Object

Although an array type is not a class, the Class object of every array acts as if:

• The direct superclass of every array type is Object.

• Every array type implements the interfaces Cloneable and java.io.Serializable.