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

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
I am not getting this question.

According to me it should be after line 9 but the answer is after line 11.

Please explain me if the answer is right then why after line 11.

Jeff Rummings
Ranch Hand

Joined: Dec 20, 2007
Posts: 42
Look at line 7:

The string is still reachable through the locObj reference. Line 11 is correct because all objects become unreachable beyond the if statement.
marc weber

Joined: Aug 31, 2004
Posts: 11343

This example is a little tricky because the object is a String literal. See Strings, Literally for an explanation.

(My understanding is that garbage collection questions on the real exam will not involve String literals.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Sheryl Pinto

Joined: Feb 26, 2008
Posts: 6
Hi Sandhi,

There is only one String object and that is "sample". anObj is just a reference pointing to "sample".

After line 7, both anObj and locObj point to "sample".
After line 9, reference anObj points to null but locObj is still pointing to "sample" and thus "sample" is not eligible for GC.

Also see:

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Thanks Arman I got it.
I agree. Here's the link:
subject: Garbage Collection
It's not a secret anymore!