aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on 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 "q on garbage collection" Watch "q on garbage collection" New topic
Author

q on garbage collection

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
At what point is the object obj eligible for garbage collection?
(Select one correct answer)
--------------------------------------------------------------------------------

public class Test058
{
public String m()
{
Object obj = new String("");
String str = null; //1
str = (String)obj; //2
obj = null; //3
return str;
}
}

--------------------------------------------------------------------------------

A: After line //1.
B: After line //2.
C: After line //3.
D: Never



The object is returned by the method and not eligible for garbage collection during the lifetime of the method.


I answered b, but the correct answer is d.

could someone please tell me y?


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
vidhya jay
Greenhorn

Joined: Aug 15, 2004
Posts: 15
Jayashree,

Object obj = new String("");
creates a new String object with obj as reference and obj is of Object Type.

str, a String reference is set to null.

obj reference variable is assigned to str and has type casted to String since str is of type String.

Now, str reference and obj reference both point to the String Object.

obj is set to null. But still we have str reference pointing to the String Object,which was created when it had the reference obj.

Hence, the Answer is D : Never.

Regards,
vidhya
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75


at line 2. object reference "str" and object reference "obj" pointing same object.
at line 3. object reference "obj" = null.
but object reference "str" still refering to (have address of) that object.

hope it's helpfull to understand.


thanks & regards,<br />Sanjeev.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: q on garbage collection