A primitive data type is used to store a single value of the specified type. An array allows us to store multiple values of the same type.


Array Fundamentals

An array is a data structure that stores multiple values of the same type. The length of an array is the maximum number of values it can store; its capacity. Once an array has been created, its length is fixed and cannot be altered. When using an array, it is not necessary to fill it to capacity. However, unused capacity represents wasted memory since the memory cannot be used for anything else. When an array is not filled to capacity, a separate integer variable is usually used to keep track of how many values are currently in the array.

Each element in an array is accessed by its position within the array. The first element is at position 0, the second at position 1, and so on. The last element is at position length-1. An array of length 10 is illustrated below:

Declaring an Array Variable

Here is the syntax for declaring an array variable:

type[] arrayId;

where type specifies what kind of values will be stored in the array and arrayId is the variable identifier. The square brackets indicate that the identifier refers to an array rather than a single value. Here are some example declarations:

int[] intArray;      // An array of integers
float[] floatArray;  // An array of floats
String[] strArray;   // An array of String objects

Declaring an array does not allocate memory for the array. Memory is allocated only when you create the array.

Creating an Array

An array is created using the new operator:

arrayId = new type[length];

This is similar to the syntax used to construct new objects. The length is an integer value. Here are some examples:

intArray = new int[10];
floatArray = new float[1000];
strArray = new String[5];

Creating an array of primitive values allocates memory for the specified number of values. The integer array has enough memory allocated to store 10 integer values. The array of floats has enough memory to store 1,000 float values.

Creating an array of objects (such as strArray in the third example) allocates memory only for the object references (which are all null immediately after the array has been created). The third statement above, allocates memory for 5 String references. Initially, each reference is null (a value of 0).

Creating an array of objects does not allocate memory for the actual objects. You must assign to each element in the array a valid object reference before you can access the corresponding element.

An array can be declared and created in a single statement as illustrated here:

int[] intArray = new int[10];
float[] floatArray = new float[1000];
String[] strArray = new String[5];

Accessing an Array Element

An array element is accessed by enclosing its index within square brackets following the array identifier:


Here are some examples:

intArray[0] = 14;
intArray[4] = intArray[0]

In the first example, the first element in the array (at index 0) is set to 14. In the second example, the value currently stored at index zero is copied and assigned to the element at index four.

Getting the Length of an Array

The capacity of an array (its length) can be obtained as follows:


Do not include empty parentheses after length. The identifier length refers to a public data member; not a method.

Initializing an Array

An array can be initialized when it is created or later on using a looping structure or the fill method. When an array is created, each element is initialized with a value of zero. If you want the array to have a different set of initial values, Java provides a shortcut way to declare, create, and initialize an array in a single statement. Here is an example:

int[] intArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

The initial values of the elements in the array are enclosed within braces. The number of values in the list determines the length of the array.

Another way to initialize an array is to use a looping structure that cycles through the positions in the array assigning each element its initial value. In the example code below each element in the array is set to zero. (This code would not be needed for a new array but would be useful if we wanted to reset all of the array values to zero.) The identifier i is an integer variable and is used to store the index into the array.

More commonly, a for loop would be used to accomplish the same task:

int i;
int[] intArray = new int[6];

for (i = 0; i < intArray.length; i++)
    intArray[i] = 0;

Note: The statement i++ is a shortcut for i = i + 1.

Yet another way to initialize an array is to use the fill method in the Arrays class. This method has two parameters; the name of the array and a value. Each element in the array is set equal to the specified value. In the example below each element in the array will be set to zero.

import java.util.Arrays; // You'll need this import statement
Arrays.fill(intArray, 0);

An Array of Objects

An array can be used to store objects (reference types) as well as values of  primitive types. In an array of objects, each element in the array stores a reference to an object. This reference is the address at which the object itself is located in memory. Declaring and creating an array of objects is no different than declaring and creating an array of some primitive data type:

classId[] classArray;
classArray = new classId[length];

You can also declare and create the array in a single statement:

classId[] objectArray = new classId[length]

For example, the code below declares and creates an array with enough room to hold 10 String objects

String[] strArray;
strArray = new String[10];
String[] strArray = new String[10];

Creating an array of objects allocates memory to store the specified number of object references. However, no memory is allocated for the corresponding objects. It is vitally important to properly initialize the elements in an array of objects before attempting to access them. Just as you did with primitive arrays, you can use a loop to initialize every element in the array:

for (i = 0; i < object.length; i++)
    objectArray[i] = new classId();

Each element in the array is assigned the address of a newly constructed object of the corresponding class. Here is a specific example for the array of string objects declared and created above:

for (i = 0; i < strArray.length; i++)
    strArray[i] = new String();