• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

MindQ question 36 garbage collection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic