aspose file tools*
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
Author

garbage collection

Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
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
also
37. Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tosin Adedoyin:
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

0

37. Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

A, B, C


SCJP Tipline, etc.
Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
Hi corey
thanks but i thought the ans willbe 1 cos at least name will be because it is set to null how do you know when they will eligble is there a hard and fast rule
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In most cases, you would be right. However, String literals behave differently when it comes to garbage collection. There is a reference created to them from a special table so they are never eligible for garbage collection. Therefore, the answer is 0. Take a look at this thread for a good explanation of how this works.
Corey
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
Hi corey,
I think the answer should be 1
as "Nick" has no references to it,it is eligible for GC.
I thought that i am confident about string gc.With your answer i am confused .
Please help.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Notice that all of the Strings in this example are "String literals." There have been a number of threads going around here lately discussing the way String literals are handled. In short, thet aren't garbage collected because, even though you don't see it, there is still a reference to that object.
Check out this thread for a good discussion of the garbage collection of String literals.
I hope that helps,
Corey
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
Hi corey,
I gone through the link you gave,
the strings created like this are not Garbage collected
1)String="NICK";
but string created like this can be GC
2)String=new String("NICK");
Is it true.
I did many mock exam and the answers given are strings created like in 1 are garbage collected.
what should i select if I get a similar question.
I am giving the exam on sunday I am confused right now.
thanks.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tony kunds:
Hi corey,
I gone through the link you gave,
the strings created like this are not Garbage collected
1)String="NICK";
but string created like this can be GC
2)String=new String("NICK");
Is it true.
I did many mock exam and the answers given are strings created like in 1 are garbage collected.
what should i select if I get a similar question.
I am giving the exam on sunday I am confused right now.
thanks.

Basically, that's true. The funny thing is that you've used a String literal to create the second String object, so you really have two different String objects - one of them in the literal pool (which you don't have a reference to, although you can get one) and one that you reference from the variable. The one in the literal pool would never be garbage collected while the new one would be eligible.
If you get a question like that on the exam, remember that literals are not garbage collected. That's the way Java works. If a mock exam says that they are, they're wrong.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection