File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 ?

Vaughn Kimmins

Joined: May 23, 2002
Posts: 3
This following question is from JavaCaps ( and I was wondering if the answer they gave is correct.
Here is the question:
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
[a] 1
[b] 2
[c] 3
[d] 0
The answer they gave is [a] 1. Is this correct? I thought it would be zero. Wouldn't string1 not be eligible for garbage collection since it now is assigned string2?
Thanks in advance!
Thomas Kijftenbelt
Ranch Hand

Joined: Feb 13, 2002
Posts: 73
In principle the answer is correct:
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
Objects are ellegible for garbage collection as soon as there is no reference to the object anymore, so
in line 3, the reference to "Test" is set to null. in line 4 string1 refers to "Today", so there is no reference to the string object "Test" anymore, which would make it ellegible for garbage collection.
However keep in mind that (but this is probably besides the point of this particular question):
- you can not guarantee that objects are garbage collected, you can only state that an object is ellegible for garbage collection
- string literals behave differently -> in fact string literals are created in a pool of string literals, and they are not ellegible for garbage collection.

[ May 26, 2002: Message edited by: Thomas Kijftenbelt ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In the exam there won't be questions about recollecting string literals, but there will be about normal Strings.

SCJP2. Please Indent your code using UBB Code
Vaughn Kimmins

Joined: May 23, 2002
Posts: 3
Okay, so I came across another question:
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
The answer they give is 1. But I thought it would've been 2 (Nick and Frieda) following the results of the other question I posted... I am so confused.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
As weird as this may sound, the correct answer is 0. String literals are never garbage collected. There is a special reference kept for each String literal from a "String constants table." You can find a good explanation of it in this thread. If you have any more questions, please ask.
In addition, this thread displays a good strategy for solving these problems.
[ May 29, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
I agree. Here's the link:
subject: garbage collection ?
It's not a secret anymore!