Java Data Type Tutorial - Java Array

What Is an Array?

An array is a fixed-length data structure to hold more than one value of the same data type.

Placing [] after the data type or after the variable name in a variable declaration makes the variable an array.

For example,

int myID;

is a simple variable declaration. Here, int is the data type and myID is the variable name.

Array Declaration

This declaration means that the myID variable can hold one integer value. Placing [] after the data type in the above declaration as in

int[] myID;

makes myID an array variable. The above declaration is read as "myID is an array of int." We can make the myID variable an array by placing the [] after the variable name, like so:

int myID[];

Both of the above declarations are valid.

We cannot specify the number of values an array can hold at the time we declare the array.

We can declare an array to hold multiple values of a data type. The following are more examples of array declarations:

// salary can  hold  multiple float  values 
float[] salary;

// name can  hold  multiple references to String objects
String[]  name;

// emp can  hold  multiple references to Employee objects
Employee[]   emp;

Arrays Creation

The general syntax for array creation is as follows:

The following code creates an array object of type ArrayDataType of Arraylength length

new ArrayDataType[Arraylength];

To create an array to store five int ids:

new int[5];

In this expression, 5 is the length of the array. It is also called the dimension of the array.

An array with more than one dimension is called a multi-dimensional array.

We can also combine the declaration of an array and its creation in one statement as

int[] myID = new int[5];

Because all arrays are objects, their references can be assigned to a reference variable of the Object type. For example,

int[] myID = new int[5]; // Create an  array object
Object obj  = myID;              // A  valid  assignment

Explicit Array Initialization

We can initialize elements of an array explicitly when we declare the array or create the array object using the new operator.

The initial values for elements are separated by a comma and enclosed in braces {}.

// Initialize the array at the time of declaration 
int[] myID = {1,   2, 3, 4, 5};

This code creates an array of int of length 5, and initializes its elements to 1, 2, 3, 4, and 5.

The length of the array is the same as the number of values specified in the array initialization list.

A comma may follow the last value in initialization list.

// A  comma   after the   last  value 5  is valid.
int[] myID = {1, 2, 3, 4, 5, }; 

Alternatively, we can initialize the elements of an array as shown:

int[] myID = new int[]{1, 2, 3, 4, 5};

For a reference type array, we can specify the list of objects in the initialization list.

The following code illustrates array initialization for String.

String[] names = {new String("A"), new String("B")};


String[] names = {"C",  "D"};

Enhanced for Loop for Arrays

We can use for-each loop with array. The syntax is as follows:

for(DataType e  : array)  {


The following code uses a for-each loop to print all elements of an int array:

int[] numlist = {1, 2, 3};
for(int num : numlist)  { 

You can accomplish the same thing using the basic for loop, as follows:

int[] numlist = {1, 2, 3};
for(int i = 0; i < numlist.length; i++) { 
   int num = numlist[i]; 

Class of an Array Object

Arrays are objects. getClass() method from the Object class returns the reference of the class.

The following code illustrates how to get the class name of an array.

public class Main {
  public static void main(String[] args) {
    int[] iArr = new int[1];
    int[][] iiArr = new int[1][1];
    int[][][] iiiArr = new int[1][1][1];
//  w  w  w .j  a  v a2  s . c o m
    String[] sArr = { "A", "B" };
    String[][] ssArr = { { "A" }, { "B" } };
    String[][][] sssArr = {}; // A 3D empty array of string

    // Print the class name for all arrays
    System.out.println("int[]:" + getClassName(iArr));
    System.out.println("int[][]:" + getClassName(iiArr));
    System.out.println("int[][][]:" + getClassName(iiiArr));
    System.out.println("String[]:" + getClassName(sArr));
    System.out.println("String[][]:" + getClassName(ssArr));
    System.out.println("String[][][]:" + getClassName(sssArr));
  public static String getClassName(Object obj) {
    Class c = obj.getClass();
    String className = c.getName();
    return className;

The code above generates the following result.

The class name of an array starts with left bracket(s) [.

The number of left brackets is equal to the dimension of the array.