Java Array Variables
Description
A Java array variable has two parts: array type and array object.
The array type is the type of the array variable. The array object is the memory allocated for an array variable.
When defining an array we can first define the array type and allocate the memory later.
Syntax
You could declare the integer array variable myIntArray with the following statement:
int[] myIntArray; // Declare an integer array variable
Note
The variable myIntArray is now a type for an integer array. No memory has been allocated to hold an array itself.
Later we will create the array by allocating memory and specify how many elements it can contain.
The square brackets following the type indicates that the variable is for an array of int values, and not for storing a single value of type int.
The type of the array variable is int[].
Alternative Syntax
We can use an alternative notation for declaring an array variable:
int myIntArray[]; // Declare an integer array variable
Here the square brackets appear after the variable name, rather than after the type name.
This is exactly equivalent to the previous statement. int[] form is preferred since it indicates more clearly that the type is an array of values of type int.
The following two declarations are equivalent:
int a1[] = new int[3];
int[] a2 = new int[3];
Array create
After you have declared an array variable, you can define an array that it references:
myIntArray = new int[10]; // Define an array of 10 integers
This statement creates an array that stores 10 values of type int and
stores a reference to the array in the
variable myIntArray
.
The reference is simply where the array is in memory.
Single Statement
You could also declare the array variable and define the array of type int to hold 10 integers with a single statement.
int[] myIntArray = new int[10]; //An array of 10 integers
The first part of the definition specifies the type of the array. The element type name, int in this case, is followed by an empty pair of square brackets.
The part of the statement that follows the equal sign defines the array.
The keyword new indicates that you are allocating new memory for the array, and int[10] specifies that the capacity is 10 variables of type int in the array.