This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Confusion !

 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 242
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler compiled exactly what was written, which you described correctly earlier.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 242
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

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

Thanks,
Srikkanth
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic