10.1
Array Types
ARRAYS
10.1 Array Types
An array type is written as the name of an element type followed by some number
of empty pairs of square brackets
[]
. The number of bracket pairs indicates the
depth of array nesting. An array's length is not part of its type.
The element type of an array may be any type, whether primitive or reference.
In particular:
Arrays with an interface type as the component type are allowed. The ele
ments of such an array may have as their value a null reference or instances of
any class type that implements the interface.
Arrays with an
abstract
class type as the component type are allowed. The
elements of such an array may have as their value a null reference or instances
of any subclass of the
abstract
class that is not itself
abstract
.
Array types are used in declarations and in cast expressions ( 15.15).
10.2 Array Variables
A variable of array type holds a reference to an object. Declaring a variable of
array type does not create an array object or allocate any space for array compo
nents. It creates only the variable itself, which can contain a reference to an array.
However, the initializer part of a declarator ( 8.3) may create an array, a reference
to which then becomes the initial value of the variable.
Because an array's length is not part of its type, a single variable of array type
may contain references to arrays of different lengths.
Here are examples of declarations of array variables that do not create arrays:
int[] ai;
//
array of
int
short[][] as;
//
array of array of
short
Object[] ao,
//
array of
Object
otherAo;
//
array of
Object
short s,
//
scalar
short
aas[][];
//
array of array of
short
Here are some examples of declarations of array variables that create array
objects:
Exception ae[] = new Exception[3];
Object aao[][] = new Exception[2][3];
int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
char ac[] = { n , o , t , , a , ,
S , t , r , i , n , g };
String[] aas = { "array", "of", "String", };
194
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved