Monday 18 July 2011

some thing about array .

array the smallest but i think the hardest thing for a learner .
why ... in every question i am related with why ?

in java array is simply different from c .
first i want to explain array in c .

int a [4] : meaning of this line

you create a box ... inside this box you divide it into 4 parts ... now after dividing you say that these all are of int types ...
so you can say an int array generally ...or a primitive array ...
why i am saying primitive array ...

                              int                  a                     [4]
                                         array-name

now your array is type of int and in int every thing is int so these all values are int type .
thats why this is int array , or primitive array .

..........................now please come in my field and concentrate ,

in java ... you or sun are always different .. why ..
because you are from c level .. and they are still from mighty sun ..
now why they are different .
they remove your all wasting theory and put them into dustbin .
and ...
   for this they focus on memory

in c u did not focus on memory ..
why focus on memory .. because in java JVM takes 1/3 of ram ( physical memory ) in my laptop with 3 gb ram it takes nearly 980 mb .
so focusing on memory is necessary for java ..


now come in task .... the arrray .

int [] a = new int  [];
this is the syntax for saving memory in array .

so what is the meaning .

here int a [] is changed to int [] a why ..
look every thing in java have some reason ....

in java for saving memory we use object ....
so object is created in heap .. and when program ends the whole space allocated to object is cleared by garbage collector .


so what we do for it

     in java in reference variable we store the address of the first value in heap . means this is the starting index of array in heap ... so for this we only need starting index to accessing any thing .
and we provide it to  ' a '

now we did this task

int          [    ]               a                 =              new             int                   [ 4 ]
                          ref-name


when we say that new is their than every thing relating it will be the part of heap . when JVM finds new at that time JVM say to new that allocated some space for the array . and it allocate the space . and then
it says to int [ 4 ] that install or fill the allocated space .

now a question can arise in your mid that why .. when you create a array in a heap than there shold be nothing in the array .. and your reference varible is not showing any error that variable is not initialzed

for this question the answer is explained previously that , we provide reference in the form of  r-value to it . r-value means that right value of variable is assigned to reference varibale thats why it is not showing any error

so till now  ...  every thing is clear to new now when new finds that every thing is clear ... than it gives the address to reference variable ...

so after this whole process their is array ... with default values which is 0 for all integers ...

1 comment:

Unknown said...

Bhai samajh mein to kuch ni aata yee mere....bss pad k accha lagta h kii bhai mehnat kr raha hai :) :)))))))))))))keep it up bro .