19 Jun 2012

How Java array is an object.


Every person who has the java basics clear would agree that array is an object. I too agree but have you given a thought about it ? How does an object can be created of primitive type when creating a primitive array.
In this article i am going to deconstruct that phrase “Array is an object”. But before I do that this is a brief view of what class and objects are all about
An object is a instance of a class. For example String is a class in java.lang package and  one may create the instance of String class like
String stringObject =new String(“java”);
Here String is a class and stringObject is an reference variable pointing to the String object. There is a corresponding relation between a class and its object.My point is to create an object there needs to be a class from which it can be created.

What about the array ,  from which class one can create array object ?

int[] arrayObject = new int[3];
To which class that arrayObject belongs to?  As we have seen in case of String class , there is a corresponding relation between a class(java.lang.String) and its object(stringObject). Certainly There is no int[] class  in whole java api.
Before i go further let me point very intersting point in Java Specification 1.7 which says
Java Specs  4.3.1  '"An object is a class instance or an array." 
 This statement clearly distinct between a class’s instance and a array. It is clear that the stringObject and arrayObject are objects but not the same since stringObject is a class's instance and arrayObject is an array.

So how does an array become an object? Here is the answer.
An array consists of its component elements. For example int is an component element  of  arrayObject. A component type,
T,  can be primitive or reference. If component type of array is T ,then type of an array itself is T[].
So it means that the type of arrayObject is int[].



Can we prove that ? of course.Lets see some code :
                       int[] val = new int[2];
                      val[0]=44;
                      val[1]=55;
                     System.out.println(val instanceof int[]); //true

It proves that the int[] is a class  But it does not exist in java api.  Where does it come from ?
The Gotcha is that the  class structure for array is built-in thing in compiler (JVM). This class only comes into play when compiler encounters an array in the code.
I would like to rephrase the term “Array is an object” to “Array is a dynamically created object”
There is a special way in which JVM represents an array of both primitive and reference types.


Lets see some code :
                       System.out.println(val.getClass());
                       System.out.println(val[].class);
                       System.out.println(Class.forName("[I"));
All above line prints : “[I”.
Where “[I” is a run-time signature for class object of int[].



And that is how an array becomes an object , not just a object but a  dynamically created object. I hope you must have learned something new about array, as I have.