• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Garbage collection

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code I came across while seeing earlier posts regarding garbage collection in javaranch forums.
class Riddle
{

public static void main(String[] args)
{
String first, second;
String riddle;
if (args.length < 2)
return;
first = new String(args[0]);
second = new String(args[1]);
riddle = "When is a " + first;
d:first = null;
riddle += " like a " + second + "?";
e:second = null;
System.out.println(riddle);
h:args[0] = null;
i:args[1] = null;
System.out.println(args[0]);

}
}
Select the one right answer.
1)d:
2)e:
3)h:
4)i:


I think the right answer is [1] that is, after the line d in executed.
Please clarify if I am wrong.
thanks
mrudul
[ December 12, 2003: Message edited by: mrudul joshi ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please state what the question is asking?
Thought of giving it a shot myself.
Thanks.
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry I didnt write the question earlier!
It is,
When will the object referred by the reference variable "first" be eligible for garbage collection?
Thanks
Mrudul
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mrudul,
I think d is the right answer.
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that my earlier ans was not clear.
I mean the option 1) (after line d)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain how is the answer [1] ?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The common theme is that these answers all have in common is that you are reassigning NULL to the object. Since we can't have one object reference (variable name) pointing to two different locations in the heap, the existing object is no longer needed, and therefore, can be flagged for garbage collection. Once you understand that, it is a matter of seeing which object is released (has null assigned to it) first in the order in which the statements will be executed. That's why it is [1].
Better?
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic