aspose file tools*
The moose likes Beginning Java and the fly likes Array Confusion ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Confusion !" Watch "Array Confusion !" New topic
Author

Array Confusion !

Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
Hi folks,

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 !


God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
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,
Srikkanth
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
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,
Srikkanth


THanks Srikkanth

for early response and better explanation!
Mario David
Greenhorn

Joined: Oct 22, 2009
Posts: 1
Hi, what happened in that program is after you created arr2 is you just told the compiler that arr2 is equal to arr Remeber, the compiler reads line by line, so after the you created the arr2 with the index of 4, you equalized it to arr, so arr2 now has a new value. thats why when you accessed arr2[4], the output is 5.

thats it,...
hope it helped you,
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Hello Mario !! Welcome to Javaranch !!

I think what you mean is the JVM which executes this .Compiler is pretty ignorant about all this I guess.

Thanks,
Srikkanth
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
The compiler compiled exactly what was written, which you described correctly earlier.
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185


Hi,

Do you mean the new int[4] object at line 2 was not created at all?

Thanks,
Srikkanth
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Srikkanth Mohanasundaram wrote:

Hi,

Do you mean the new int[4] object at line 2 was not created at all?

Thanks,
Srikkanth


The int[4] object is created and a reference to it is assigned to arr2.

Then arr2 is assigned another reference and this means that the int[4] object is no longer referenced so it's eligible for GC.
 
jQuery in Action, 2nd edition
 
subject: Array Confusion !