Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic

garbage collection

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Question1 :

After method m1 returns, the object created on which line is eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. Compile-time error
f. Run-time error
g. None of the above

Answer: g (none of the above)..

Question 2:

Which of the following is a true statement?

a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. The program compiles, runs and prints X.
d. The program fails to compile.
e. None of the above

Answer: c.

Can anyone explain why a1 is not elligible for garbage collection in Question1?

And why "i" is not elligible in Question 2?

Thanks in advance..


(code tags added - please learn how to do this)
[ May 19, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For your first question: remember that the reference a1 in the method m1 is a copy of the reference a1 created in the main method. Setting a1 to null in m1 does not set the a1 in main to null. Compare with passing a primitive value such as an int.

The same analysis holds for your second question.
[ May 19, 2004: Message edited by: Barry Gaunt ]

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

Joined: Apr 28, 2004
Posts: 60
But passing a primitive is different from passing a reference...

am i right?

David Ng

Joined: May 11, 2004
Posts: 12
The reference itself is passed just like a primitive. You can modifies the field refered to by the reference but any changes made to the reference stays local.
Anuradha Shanmugam

Joined: May 09, 2004
Posts: 8
Hi Rajani,

Why dont you have a look at this wonderful flash example given by Corey @ ?


Anuradha<br />(SCJP 1.4)
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
In both the cases you are passing a reference to the methord m1. Inside method m1 there is a local reference a1 and i respectively. They are local to the methord m1 and does not have any relation with the instance reference. There reference get created inside the method m1 and loses its scope once the method finishes execution.

So if you make the local reference as null then only the local reference will not point to the required object. The instance reference continues to refer to the object.

The case is different if you try to change the value of the object. since the instance reference and local reference are pointing to the same object modification is possible to the object with both the reference.

Hope it is clear.

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou soo much all of you for your replies..

I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!