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

Garbage Collection

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
At what stage in the following method does the string "abc" becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = "abc";
r = r+1; //1
r = null; //2
s = s + r; //3
} //4

a) Before statement labelled 1
b) Before statement labelled 2
c) Before statement labelled 3
d) Before statement labelled 4
IMO answer should be c, bcoz the object reference has been made equal to null at line 2 but actual answer is d, I don't knbow how. Can anybody please explain me.
Thanx in advanced.
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
It's possible the question refers "abc" to the String literal (s). I don't know, however, if String literals are eligible for gc.
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
i think theres some confusion in the questionairre. you have not specified
which string "abc" becomes available for garbage collection.
keep in mind that two separate string objects are created when
you use
String r = new String("abc");
String s = "abc";
in this context, "abc" referenced by r will become available for garbgare
collection at line 1.
similarly, line 3 for "abc" referenced by s.
i think question demands GC availability for s object
I agree. Here's the link:
subject: Garbage Collection
Similar Threads
Garbage collection
literal pool - javaprepare
Garbage Collection
Garbage Collection
GC doubt