Monday 18 July 2011

how the mighty JVM knows allocate space for array where constructor is not defined.

i want to ask a question to you that , we all are reading java from 3-4 years now explain me how space is allocate when you create a array .

                        int [ ] arr = new int [ 4 ] ;

tell me here how space is allocate when , there is no constructor ?

so i think for me . for today this is the best question for a learner . 

now i will provide solution to you . but please dont accept it as a final , because it is my opinion and always opinions are wrong in JAVA .

so lets start . when you define int  [ ] arr ;
at this time you say that .... hola i have a reference ... which is pointing to something which is group of same values .... you can call it a Array .

now if i say are you happy with it . if you are starter than you say .. ok  but i am not a starter i want to learn things provide me things . 

now the sentence i am saying that " i am learner and want something " this case is also allowed in case of array . but how is the question .

for this you have to read some the mighty concept of java .. the " new " 
when i write -----------------------------------------------------------------------------:" new int  [  ] "
what happen can you think .

for this dig into this part . whenever JVM finds new , the instruction for JVM is that now its to allocating something , but what than your syntax says that there is a int type of something , but do you think that this is ok , without asking syntax how much .
its like a example a requester goes into bank and say i need money , and the bank say to him sir please go to the store and take money from there , without asking his need .

so when we define syntax , it is necessary that defining the size also there .
now every thing is ok . but where is the topic's matter ,,, ya now my task is started ... read it carefully ...

when your array is initialized in heap , but the point is that everything is heap is in form of  '0' if you taking it . but surely now your question is that why ' 0 ' i dont want to provide any value .. but my boy 
here you will work for  java  and in its things ..not for me ,and not yourself .

so why this ... because in INTEGER class ,,, if you have a time , than please walk inside the integer class
there you find that it have a constructor where providing value is necessary . thats why if we dont provide value to our used thing than always it shows error that , value is not assigned .. but why this thing is related to array .

now you have a array with by default values ' 0 ' . 
so now its time for wrapping .
wrapping is used when you want to create a object means you are using heap , for space saving .
so for int you use a INTEGER class . now INTEGER is wrapped class of int . so what is the work of this INTEGER . 

listen i told you that , we have by default values with all ' 0 ' s .
so when i say that your array is holding 4 values  , and each of this value is 0 , at that time the INTEGER class takes this 0 with an int , to allocating the space , space allocation instruction comes from " new " so when the whole task is completed , means by iterating the array , because size is defined and here we use Iterator() & hasNext() method . for finding the next value of the next variable . 

so JVM  will allocate nearly 16 bytes for  4 values and thats all for this topic . and thanks to god to helping me in this topic & for creating these all different things in my mind ... and thanks to you for opening it ...and thanks to google for providing a way to expressing ....

No comments: