wood burning stoves
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

Garbage collection

mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
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)
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;
h:args[0] = null;
i:args[1] = null;

Select the one right answer.

I think the right answer is [1] that is, after the line d in executed.
Please clarify if I am wrong.
[ December 12, 2003: Message edited by: mrudul joshi ]
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Could you please state what the question is asking?
Thought of giving it a shot myself.
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
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?
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Hi Mrudul,
I think d is the right answer.

Thanks,<br />Lakshmi.
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Sorry that my earlier ans was not clear.
I mean the option 1) (after line d)
Diwakar Bhandari

Joined: Dec 15, 2003
Posts: 1
Can someone explain how is the answer [1] ?
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
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].
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Garbage collection
It's not a secret anymore!