Search This Blog

17 June, 2011

Array in java part 2

Part:- 2


In case of array in c/c++ a pointer is created implicitly and values can get assigned to locations out bounds, hence java doen't support array like c/c++.


But if sun Microsystems doesn't provide support for array then it would be a problem for a java programmer because array is a tool which provides the feature of managing a large collection with same id and different index.
Because of this reason java supports array but in a different fashion. In Java special treatment is given to the code which uses array.
The code is entirely changed by the compiler .


Look at the following code.


In the above example my assumptions are three words i.e. IntCollection, setValue and getValue.
Wherever the compiler found [] in the source code, the compiler changed it to either IntCollection , setValue or getValue.


The technical explanation is as follows
int x[]  a class pointer gets created 
x=new  a class pointer gets created
x=new int[5] an object of a class is created and 5 is passed to its parameterized constructor, and because of the constructor the object that gets created contains 5 locations for storing int type values. The object also contains a property is public as well as final. Its value can be accessed but cannot be changed.
x[0]=33 a setter method gets invoked on the object and the method is being passed  0 and 33 as arguments. The setter method assigns 33 to the first block out of the five blocks created to store int type value.


System.out.println(x[0]) a getter method is invoked on the object and the method is being passed 0 as argument. The method returns the value of the first block out of the five blocks.
In short its said that in java array is treated as an object. 
If a value is being assigned to a location out of bounds then code will get compiled be it won't get executed.


<<<< Array in java Part :-1


 Resizing an array>>> >

No comments:

Post a Comment

Meetme@