This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara 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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Roll
Ranch Hand

Joined: Jan 20, 2000
Posts: 52
Q]Which is the earliest line in the following code after which the object created on the line marked(0) will be a
candidate for being garbage collected, assuming no compiler optimization are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b= "bye"; //0
String c = b + "!"; //1
String d = b;
b = a; //2
d = a; //3
return c; //4
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); //5
}
}
A)The line marked(1)
B)The line marked(2)
C)The line marked(3)
D)The line marked(4)
E)The line marked(5)
Is it line3) because line0) hets garbage collected after line2).
So, i think c) should be correct , but some say's it as B).
Any comments or suggestions?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20545
    ∞

I'll go with B because in the question it says "Which is the earliest line in the following code after which ..." For me the "after which" makes all the difference.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think it should be C) line three. Here is my reasoning:
The object created in line 0 is a String object containing the letters "bye". Later, the variables b and d both refer to this object. After line two, the variable d still refers to this object, so it can't be garbage collected. After line three, there are no variables referring to that String object, so it could be garbage collected.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20545
    ∞

Excellent point. You are correct.
Just to mess things up a bit more... The question is obviously designed to find a particular answer. But the question is errant. b references a string constant that is in the String Pool before the program is started. Therefore, it is never garbage collected. This particular example would be better if it had the line b = new String("bye");
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Can any java Guru explain What is meant BY "assuming no compiler optimization are done?" what implication it has on the Question??
thanks
Sharana
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
The smart optimizing compiler could replace the entire
code with the following code without affecting the results.

 
wood burning stoves
 
subject: Garbage Collection