File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

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)
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 ]
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.
Thanks.
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?
Thanks
Mrudul
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
Greenhorn

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].
Better?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection