Java Array Variables

In this chapter you will learn:

  1. What is Java array variable
  2. Syntax for Java array variable definition
  3. Note for Java array variable definition
  4. Alternative Syntax for Java array variable definition
  5. Create an array
  6. Single statement for declare and create an array

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.

Next chapter...

What you will learn in the next chapter:

  1. What are the Java Array Initial Values
  2. Initial value for Java array
Home »
  Java Tutorial »
    Java Langauge »
      Java Array
Java Array
Java Array Variables
Java Array Initial Values
Java Array Length
Java Initialize Arrays
Java Array Index
Java Array for each loop
Java Multidimensional Arrays