File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MindQ question 36 garbage collection

 
mary lee
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
36. 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 is 1
but I think should be 2, there are three String objects "Nick"
"Jason" and "Frieda", only "Frieda" is still referrenced by newestName , other two are not referrenced any more
is it right?
could anyone help?

 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason is referenced by newName
Frieda is referenced by newestName
Nick isn't referenced at all
So only 1 object is eligible
Hope that helps,
Bill
 
Haining Mu
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bill bozeman:
Jason is referenced by newName
Frieda is referenced by newestName
Nick isn't referenced at all
So only 1 object is eligible

I'm still wondering the answer or question wording. Since all strings here are literals, NOT initialized using new op., there is no way we can say how many are eligible for gc.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Haining,
What you say is true but for the purpose of answering mock exam questions that use String literals, assume they are garbage collected.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question says, "How many objects are eligible for gc?". I think when name =null, it means the object "name" as well as "newestName" (which is also assigned to name) is eligible for gc. I think, here, two objects are eligible for gc.
Please correct me if I am wrong........
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Muhammad,
"name" is a reference variable, not an object.
Java has two types of variables: primitive type and reference type.
A primitive type variable ie <code>int i</code> actually contains the value that is assigned to it.
<pre>
Memory
---------
int i = 10 -> | 10 |
---------
</pre>
A reference type variable ie <code>String name</code> contains the memory address for the object that is assigned to it; not the object itself.
<pre>
Memory Memory
----------- ------------
String str = "Hello" -> | address | points to | Object |
----------- ------------
</pre>
Only the memory where objects are stored is garbage collect. The memory area containing variables is not.
Hope that helps.

Sorry ... haven't mastered ASCII art hope you get the idea.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited June 20, 2001).]
[This message has been edited by Jane Griscti (edited June 20, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic