This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Pinkal Patel
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
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


Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

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?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

Ok, I got the concept. Thanks ranchers for the help offered.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection
 
Similar Threads
garbage collection doubt
garbagecollection
Doubt in GC
Garbage Collection
Dan exam doubt 19