Skip to main content

14. Array

 

Declaring an Array

Arrays can be declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

        int[] array; // Recommended
        int [] array; // Legal
        Object array []; // Legal but less readable
        String array[]; // Legal but less readable

We can also declare multi-dimensional arrays.

        String[][][] array; // Recommended
        String[] array[]; // Legal but less readable

When an array of objects is instantiated, objects within the array are not instantiated automatically, but all the references get the default value of null.

It is never legal to include the size of the array in our declarations. The JVM doesn't allocate space until we actually instantiate the array object. That's when size matters.

class ShellClass {
    public static void main(String[] args) {
        int[5] array;
    }
}

Constructing an Array

To create an array object, Java must know how much space to allocate on the heap, so we must specify the size of the array at creation time. The size of the array is the number of elements the array will hold.

Constructing One-Dimensional Arrays

To construct an array, use the keyword new followed by the array type, with a bracket specifying how many elements of that type the array will hold.

        int[] array = new int[5];

The preceding code puts one new object on the heap - an array object holding five elements - with each element containing an int with a default value of 0.

Arrays must always be given a size at the time they are constructed. The JVM needs the size to allocate the appropriate space on the heap for the new array object.

class ShellClass {
    public static void main(String[] args) {
        int[] array = new int[];
    }
}

Objects can still be created even without using or seeing the keyword new.

        int[] array = { 1, 2, 3, 4, 5 };

Constructing Multi-Dimensional Arrays

A two-dimensional array of type int is really an object of type int array (int[]), with each element in that array holding a reference to another int array. The second dimension holds the actual int primitives. The dimensions in a multidimensional array can have different lengths.

        int[][] array = new int[3][];
        array[0] = new int[2];
        array[1] = new int[3];

Anonymous Array

Anonymous array creation can be used to construct and initialize an array, and then assign the array to a previously declared array reference variable.

        int[] array;
        array = new int[] { 1, 2, 3, 4, 5 };

We do not specify a size when using anonymous array creation syntax.

class ShellClass {
    public static void main(String[] args) {
        int[] array = new int[5] { 1, 2, 3, 4, 5 };
    }
}



  • If we assign an array to a previously declared array reference, the array we're assigning must be the same dimension as the reference we're assigning it to.
  • An array of primitives can accept any value that can be promoted implicitly to the array's declared type.
  • An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array.
Prev Next

Comments