Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.You have seen an example of arrays already, in the main method of the "Hello World!" application.


Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.

class ArrayDemo {
    public static void main(String[] args) {
        // declares an array of integers
        int[] myArray;

        // allocates memory for 5 integers
        myArray = new int[5];
        // initialize first element
        myArray[0] = 10;
        // initialize second element
        myArray[1] = 20;
        // and so forth
        myArray[2] = 30;
        myArray[3] = 40;
        myArray[4] = 50;

        System.out.println("Element at index 0: "
                           + myArray[0]);
        System.out.println("Element at index 1: "
                           + myArray[1]);
        System.out.println("Element at index 2: "
                           + myArray[2]);
        System.out.println("Element at index 3: "
                           + myArray[3]);
        System.out.println("Element at index 4: "
                           + myArray[4]);

Array Declaration

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the variable naming section. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type.

type[] variableName;
type variableName[];

Both are valid syntax for array declaration, but I prefer former as is more readable.I feel it is easier to read the code when the square brackets are placed right after the data type in the array declaration.The below java code declares an array (named myArray):

// declares an array of integers
int[] myArray;

Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
boolean[] anArrayOfBooleans;
String[] anArrayOfStrings;

Initialization, Accessing an array

When you declare a Java array variable you only declare the variable (reference) to the array itself. The declaration does not actually create an array. 'new' operator is used to initialize an array. You create an array like this:

int[] myArray;
// create an array of integers
myArray = new int[5];

If this statement is missing, then the compiler prints an error like the following, and compilation fails:
Variable myArray may not have been initialized.
This example creates an array of type int with space for 5 int variables inside.Once an array has been created its size cannot be changed.

The previous Java array example created an array of int which is a primitive data type. Java allows you to create an array of references to any type of object (to instances of any class).For instance:

String[] myArray = new String[5];

The next few lines assign values to each element of the array:

myArray[0] = 10; // initialize first element
myArray[1] = 20; // initialize second element
myArray[2] = 30; // and so forth

Each array element is accessed by its numerical index:

System.out.println("Element 1 at index 0: " + myArray[0]);
System.out.println("Element 2 at index 1: " + myArray[1]);
System.out.println("Element 3 at index 2: " + myArray[2]);

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[] myArray = { 10, 20, 30, 40, 50 };

Here the length of the array is determined by the number of values provided between braces and separated by commas.

Copying Arrays

The System class has an arraycopy method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src, int srcPos,
                             Object dest, int destPos, int length)

The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

The following program, ArrayCopyDemo declares an array of char elements, spelling the word "decaffeinated." It uses the System.arraycopy method to copy a subsequence of array components into a second array:

class ArrayCopyDemo {
    public static void main(String[] args) {
        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
			    'i', 'n', 'a', 't', 'e', 'd' };
        char[] copyTo = new char[7];

        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        System.out.println(new String(copyTo));

The output from this program is:


Multi dimensional Array

In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:

class MultiDimArrayDemo {
    public static void main(String[] args) {
        String[][] names = {
            {"Mr. ", "Mrs. ", "Ms. "},
            {"Smith", "Jones"}
        // Mr. Smith
        System.out.println(names[0][0] + names[1][0]);
        // Ms. Jones
        System.out.println(names[0][2] + names[1][1]);

The output from this program is:

Mr. Smith
Ms. Jones

Finally, you can use the built-in length property to determine the size of any array. The following code prints the array's size to standard output: