• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Confusion !

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler compiled exactly what was written, which you described correctly earlier.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,

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

Thanks,
Srikkanth
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic