int[] a; /* declares a reference a to an interger
array of unspecified dimension */
a = new int[3]; /* creates an array object with
3 interger elements */
The declaration and creation can be combined as follows:
This creates an array of integers as follows:
A different size of interger array object can be assigned to the array reference a at any time as follows:
a = new int[256];Java's built-in automatic garbage collection service gets rid of the old dis-referenced 3-element array.
The size or length of the object currently referenced by reference variable a is always available in the length variable a.length. This is illustrated in the program below which prints out the contents of the array.
for(int i = 0; i < a.length; i++) System.out.println(i + ": " + a[i]);