← Previous Next →

An array is a collection of similar data types. Array is a container object that hold values of homogenous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.

An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.

Array Declaration


datatype[ ] variableName;
datatype variableName[ ];

Both are valid syntax for array declaration.


int[ ] arr;

char[ ] arr;

short[ ] arr;

long[ ] arr;

int[ ][ ] arr; // two dimensional array.

Accessing array element

Array element can be accessed using index which starts from 0 and end at (n-1) where n is the size of array.

To access nth element of an array use 



Example :

//To access 4th element of a given array

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

System.out.println("Element at 4th place" + arr[3]);

The above code will print the 4th element of array arr on console.


← Previous Next →