Array creation

One dimensional array creation

Every array in java is an object, hence we can create arrays by using new operator.

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[] a = new int[3];

        System.out.println(a.getClass.getName());
    }
}

Every array type has corresponding classes. However these classes are part of the java language, and not available to the programmer. You can view the class names by printing out like demonstrated above.

Arrays with their corresponding class name:

Array typeCorresponding class name
int[][I
int[][][[I
double[][D
short[][S
byte[][B
boolean[][Z

Creating an array with a specified size:

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[] a = new int[]; // INVALID: cannot specify size at declaration, but must specify size when creating array
        int[] b = new int[6]; // VALID
        int[] b = new int[0]; // VALID
        int[] b = new int[-3]; // INVALID: will compile, but fail at runtime and throw NegativeArraySizeException
    }
}

When creating an array, the JVM will reserve the memory of specified size, therefore if size is not specified, compile-time error will be thrown.

It is also allowed to create an array of size 0. An example of this is the main method, if there are no command line arguments passed into the method, the size of the args array is 0:

import java.lang.*;

class Test {
    public static void main(String[] args) {
        System.out.println(args.length);
    }
}
Valid data types for creating arrays
Diagram showing that you can assign lower data type to higher data type variable.
Diagram showing that you can assign lower data type to higher data type variable.

The allowed data type for specifying array size is int , meaning all data-types that can be assigned to int are valid data-types for specifying array size.

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[] a = new int[10]; // VALID
        int[] b = new int['a']; // VALID - will use corresponding ascii value

        byte by = 20;
        int[] c = new int[b]; // VALID

        short sh = 30;
        int[] d = new int[sh]; // VALID

        int[] d = new int[10L]; // INVALID - compile-time error: posssible loss of precision, found long, required int
    }
}

The maximum size that an array can be, is 2,147,483,647 (4 bytes), which is the maximum size of an int

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[] a = new int[2,147,483,647]; // VALID
        int[] a = new int[2,147,483,648]; // INVALID - compile-time error: integer number too large
    }
}

Although 2,147,483,647 is the largest array size, it requires 2,147,483,647*4 bytes of memory. This is a lot of memory, so ensure that the computer has sufficient heap memory to allocate to the array, otherwise there will be a java.lang.OutOfMemoryError: Requested array size exceeds virtual machine limit.

Two dimensional array creation

2D Matrix representation:

Diagram illustrating how matrix style representation array can be inefficient at memory utilisation
Diagram illustrating how matrix style representation array can be inefficient at memory utilisation.

2D Array of arrays representation:

Diagram illustrating how array of arrays style representation is efficient at memory utilisation
Diagram illustrating how array of arrays style representation is efficient at memory utilisation.

In java, two dimensional array is not implemented by using matrix style representation, java uses arrays of arrays approach for multi-dimensional array creation. The main approach of this approach is memory utilisation will be improved.

To represent the array above in java:

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[][] arr = new int[5][]; // VALID: Only base size has to be specified
        arr[0] = new int[5];
        arr[1] = new int[2];
        arr[2] = new int[1];
        arr[3] = new int[3];
    }
}

Array of arrays representation for a 3d array:

Diagram illustrating array of arrays style representation for 3d array.
Diagram illustrating array of arrays style representation for 3d array.

To represent the array above in java:

import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[][][] arr = new int[2][][]; // VALID: Only base size has to be specified
        arr[0]    = new int[3][];
        arr[0][0] = new int[1];
        arr[0][1] = new int[2];
        arr[0][2] = new int[3];
        arr[1]    = new int[2][2];
    }
}
import java.lang.*;

class Test {
    public static void main(String[] args) {
        int[]     a = new int[];  // INVALID
        int[]     a = new int[3]; // VALID
        int[][]   a = new int[][]; // INVALID
        int[][]   a = new int[3][]; // VALID
        int[][]   a = new int[][4];  // INVALID
        int[][]   a = new int[3][4]; // VALID
        int[][][] a = new int[3][4][5]; // VALID
        int[][][] a = new int[3][4][]; // VALID
        int[][][] a = new int[3][][5]; // INVALID
        int[][][] a = new int[][4][5]; // INVALID
    }
}