array declarations Example
The program shows some array declarations.An array declaration does not reserve any memory for the array to reside in, this can only be done by an array initialization or by using the new operator.
The declaration informs the compiler of the name, type and dimensionality of the array, but not its length.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Array DeclarationsExample
class ArayDeclarationsExample {
public static void main(String[] args) {
int[] ia;
int ib [];
double[] da;
float[][] fa;
String[] sa;
}
}
6 The square brackets following int indicate that the variable ia is an array. It is an array of integers.
Note that a declaration does not indicate the length of the array.
8 This declaration is an alternative form of the previous declaration.
It means exactly the same, namely an array of int is declared.
10 An array of double is declared.
Notice, like the previous two declarations, it is defined using one pair of brackets [].
This indicates a 1-dimensional array, each element of which will be accessed by a single index.
12 A 2-dimensional array of float is declared.
The number of paired brackets indicates dimensionality. A single pair for 1-dimension, 2 pairs for 2-dimensions, and so on.
An element of a 2-dimensional array is accessed by two indices.
14 An array of String is declared.
