• 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

Garbage Collection

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

The question below is from Dan Chisholm,


Now, After method m1 returns, the object created on which line is not eligible for garbage collection? Answer is Line 1

I can understand the above from the point that array references are passed by reference value and any modifications that we do to it's elements will refelect in the calling method as well.

Now considering the question below also from Dan Chisholm,



After method m1 returns, the object created on which line is eligible for garbage collection? The answer was, None of the above.

I want to know is that what is the difference between the above 2 cases. Why not the statement a1 = null in the m1(I[] a1) method not affect it in the main method?? Anyone please help me with this.

Thanks in advance.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the Program 1)



Assigned null to Actual value of the Array
but in the Program 2)

Will Null the Second Refference to Array Object

e.g Lets Change line

with


Meance Now
a1 -> OBJECT of Array
and
a2 -> OBJECT of Array
Both Reffer the Same object if you mack a2=null;
meance OBJECT of Array is not aloved for GC
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok that is understandable that it will null the second reference. I want to know, won't it affect the reference from the main method?? Why not the reference from the main method be nulled???

Any thoughts on that?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Ok that is understandable that it will null the second reference. I want to know, won't it affect the reference from the main method?? Why not the reference from the main method be nulled???

Any thoughts on that?



Remember that Java always passes a copy of the reference to the method. So it is a local reference variable that is set to null. The confusion is caused by the fact that the name of the local reference variable (in the parameter list) is the same as the one in the main method, namely a1.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I got the concept. Thanks ranchers for the help offered.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic