my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mughal rasmussen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mughal rasmussen" Watch "Mughal rasmussen" New topic
Author

Mughal rasmussen

Mahen
Greenhorn

Joined: Feb 20, 2000
Posts: 23
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 optimizations 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)
}
}

No clue! detailed explanation about GC questions like above
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
After line marked 2.
Till line 2, b has ref. to 'bye'.
At that line 'bye' lost the ref. to b and hence eligible for GC.
Am I right ?.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The object "bye" will be garbage collected after //3. This is because after the statement "String d = b;" both d and b are refering to the same object that is "bye". So after //2 b will be refering to "hello" but d will still be refering to "bye". After //3 there will be no reference to "bye" hence it will be ready for garbage collection.
josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
It is string literal and so never Gced
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
How about 'c'? 'C' is still holding reference for 'bye'. I think the object will be garbage collected only after line marked 4.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
akhil- No, c is holding a separate String, "bye!". It was created by copying characters from the original "bye" and adding "!", but now it's a completely separate String, and has no link to the original "bye".
shan- correct, since it's a String literal, it won't ever be garbage collected. That's a subtle point that isn't on the exam, but it's good to know anyway.
If the object were not a String literal, then it would be eligible for collection after line 3, as Hood says.

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Jim, when we create a string using a literal it creates a String object and we can apply all the methods of a String class on it (like "hood".equals("hood") is a valid statement) so I thought it would behave as a normal object during garbage collection also. But it seems Garbage collection takes care of objects created by the "new" operator only.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hood- not exactly. There are numerous ways to create an object without "new" - at least, without a "new" in any code that you write or see - and garbace collection works normally for all of them, except for string literals. Examples:
<code><pre>
String a = "Wallace"; // "Wallace" never collected
String b = a + " & Gromit";
b = null; // "Wallace & Gromit" could be collected
String c = a.toUpperCase();
c = null; // "WALLACE" could be collected
String d = a.substring(3);
d = null; // "lace" could be collected
String e = a.clone();
e = null; // "Wallace" (2nd) could be collected
</pre></code>

[This message has been edited by Jim Yingst (edited February 23, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
one small correction.But I understand your point.
String e =(String) a.clone();
e = null; // "Wallace" (2nd) could be collected
// the clone() method inherited from Object returns an Object. We have to cast to the corrs. type.
maha anna

[This message has been edited by maha anna (edited February 23, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Good call. Thanks.
 
 
subject: Mughal rasmussen
 
Similar Threads
que on GC
Strings and GC
Garbage
GC query
Garbage collector