Java Reference
In-Depth Information
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", };
The [] may appear as part of the type at the beginning of the declaration, or as part of the
declarator for a particular variable, or both.
For example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
In a variable declaration (§ 8.3 , § 8.4.1 , § 9.3 , § 14.14 , § 14.20 ) except for a variable arity
parameter, the array type of a variable is denoted by the array type that appears at the begin-
ning of the declaration, followed by any bracket pairs that follow the variable's Identifier
in the declarator.
For example, the local variable declaration:
int a, b[], c[][];
is equivalent to the series of declarations:
int a;
int[] b;
int[][] c;
Brackets are allowed in declarators as a nod to the tradition of C and C++. The general
rules for variable declaration, however, permit brackets to appear on both the type and
in declarators, so that the local variable declaration:
float[][] f[][], g[][][], h[]; // Yechh!
is equivalent to the series of declarations:
float[][][][] f;
float[][][][][] g;
float[][][] h;
We do not recommend “mixed notation” in an array variable declaration, where brackets
appear on both the type and in declarators.
Once an array object is created, its length never changes. To make an array variable refer to
an array of different length, a reference to a different array must be assigned to the variable.
Search WWH ::




Custom Search