Arrays in Java

In the background, every time you create an array, the compiler creates an object which allows you to:

  • get the number of elements in the array through the length field. The length or size of an array is the number of elements in it.
  • access each element by specifying an index. This indexing is zero-based. Index 0 refers to the first element, 1 to the second element, etc.

All the elements of an array have the same type, called the element type of the array. An array is not resizable and an array with zero element is called an empty array.

An array is a Java object. Therefore, an array variable behaves like other reference variables. For example, you can compare an array variable with null.

String[] names;
if (names == null)  // evaluates to true

If an array is a Java object, shouldn’t there be a class that gets instantiated when you create an array? May be something like java.lang.Array? The truth is, no. Arrays are indeed special Java objects whose class is not documented and is not meant to be extended.

To use an array, first you need to declare one. You can use this syntax to declare an array:

type[] arrayName;

or

type arrayName[]

For example, the following declares an array of longs named numbers:

long[] numbers;

Declaring an array does not create an array or allocate space for its elements, the compiler simply creates an object reference. One way to create an array is by using the new keyword. You must specify the size of the array you are creating.

new type[size]

As an example, the following code creates an array of four ints:

new int[4]

Alternatively, you can declare and create an array in the same line.

int[] ints = new int[4];

After an array is created, its elements are either null (if the element type is a reference type) or the default value of the element type (if the array contains primitives). For example, an array of ints contain zerosby default.

To reference an array element, use an index. If the size of an array is n, then the valid indexes are all integers between 0 and n-1. For example, if an array has four elements, the valid indexes are 0, 1, 2 and 3. The following snippet creates an array of four String objects and assigns a value to its first element.

String[] names = new String[4];
names[0] = "Hello World";

Using a negative index or a positive integer equal to or greater than the array size will throw a java.lang.ArrayIndexOutOfBoundsException.

Since an array is an object, you can call the getClass method on an array. The string representation of the Class object of an array has the following format:

[type

where type is the object type. Calling getClass().getName() on a String array returns [Ljava.lang.String. The class name of a primitive array, however, is harder to decipher. Calling getClass().getName() on an int array returns [I and on a long array returns [J.

You can create and initialize an array without using the new keyword. Java allows you to create an array by grouping values within a pair of braces. For example, the following code creates an array of three String objects.

String[] names = { "John", "Mary", "Paul" };

The following code creates an array of four ints and assign the array to the variable matrix.

int[] matrix = { 1, 2, 3, 10 };

Be careful when passing an array to a method because the following is illegal even though the method average takes an array of ints.

int avg = average( { 1, 2, 3, 10 } ); // illegal

Instead, you have to instantiate the array separately.

int[] numbers = { 1, 2, 3, 10 };
int avg = average(numbers);

or you can do this

int avg = average(new int[] { 1, 2, 3, 10 });