I have a confusion regarding the size of array.Here is my code
output : 5
This program runs fine but my doubt is that in #1 we initialize array with size of 5,. In #2 we initialize with array size 4. Now in #3 we assign the #2 = #1 .As far as I know arraysize can't grow while once it has been fixed but whats going behind.
Please explain !
Thanks in advance !
Srikkanth Mohanasundaram wrote:Hi Ashish,
I don't think the arraysize is being increased, the variable arr2 is now pointing to a object which was previously referenced by arr. Now what happened to the object which was referred by arr2 ? (Hmm.. waiting to be cleaned up!!!)
Hope this helps,
for early response and better explanation!
hope it helped you,
Srikkanth Mohanasundaram wrote:
Do you mean the new int object at line 2 was not created at all?
The int object is created and a reference to it is assigned to arr2.
Then arr2 is assigned another reference and this means that the int object is no longer referenced so it's eligible for GC.