by Dinesh Thakur Category: array

Array is a collection of similar elements. Array is a predefined class present in java.1ang package. It is a final class which can't be inherited. In java array holds both primitive data types as well as object references. In java when the programmer develop the array program then the programmer has to follow 3-steps.

1. Array declaration:-int a[]; here ‘a’ is a reference of

At the declaration time of the array programmer never specifies the size of the array.

2. Array construction:-

a=new int[2];

At the construction of the array programmer is bound to specify the size of the array.

3. Initialization of array:-

In java array is initialized through the index .The initialization of the array means putting elements into an array.



Representation of array in memory


1.     In java when the array is declared it captures a chunk of memory from the stack.

2.     Array is constructed at the run time. At the construction time by specifying the size of the array through new operator garbage collector allocates consecutive memory location from the heap.


Here is the Java Example for Array in Java:


public class test


public static void main(String args[])


int a[]=new int[3};








The statement "int a[]=new int[3]" is to create the integer array of length 3. Here "[]" is associated with variable declaration that means it is a reference variable and here new operator is used to allocate memory from heap. So in this example "a" is a reference type variable which means it holds the address of the memory 1ocation.The new operator allocates memory from heap according to the size specified and the starting address is stored in "a". The memory is allocated for "a" from heap.


Representation of array in memory


1.     The reference variable created inside the stack is "a". As the memory is allocated from heap, the starting address of memory location is "44446" which is stored in "a". in array continuous memory allocation is done and the integer takes 4-bytes of memory, the address of next memory location is 44450 and 44454.

2.     In java the compiler implicitly calls the new operator to allocates memory from heap for creation of an array without using new operator.




Int a[]={11,22,33};






But here if we write "System.out.println(a[3]);" then an exception will arise i.e. ArraylndexOutOfBoundsException because the elements are beyond the size of an array.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles