Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sagarika nair
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry that my earlier ans was not clear.
I mean the option 1) (after line d)
 
Diwakar Bhandari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain how is the answer [1] ?
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic