File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes gc? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "gc?" Watch "gc?" New topic


sri devij
Ranch Hand

Joined: Oct 23, 2000
Posts: 37
1.2) public class X {
2. public void m(Object x) {
3. x = new Integer(99);
4. Integer y = (Integer)x;
5. y = null;
6. System.out.println("x is" + x);
7. }
When is the Integer object, created in line 3, eligible for garbage collection?
B.just after line 4
C.just after line 5
D.just after line 6 (that is, as the method returns)
E.when the calling method sets the argument it passed into this method to null
I thought the answer for this c.Can anybody tell me which is the correct answer for this?

Joined: Aug 13, 2000
Posts: 16

I also think its C because an object is eligible for garbage collection when it refers to null.
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
sri devij
In my views the answer is D as the x is defined inside the method body and hence,its scope is limited to the method.As soon as the method completes its work,the x is availble for gc.
Sudhir Bangera
Ranch Hand

Joined: Oct 10, 2000
Posts: 50
I think Prasad is right, the answer should be D.
Peter Wei Cai

Joined: Oct 23, 2000
Posts: 2
I choose D.
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Dear Friends,
I would like to add some more concrete details to the answer.
1) The object is sent to gc only when all the references are removed for that object.
2)If two or more reference variables refer to the same object.And if any one is set to null,that does not mean that the object is set to null and sent to gc, rather the object reference is removed for that variable.
i.e if x and y refer to the object Integer, and later if u set y=null, then the object reference of y is removed not the object.
You can see this by running this example.
public class SimpleRef
public void getValue(Object x)

x=new StringBuffer("Hello");

StringBuffer y=(StringBuffer)x;

y.append("Sun"); //later comment this
//y=null; //later uncomment this & see the result.

System.out.println("The value of x =" + x);
System.out.println("The value of y =" + y);

public static void main(String[] args)
SimpleRef myRef=new SimpleRef();
StringBuffer c=new StringBuffer("from main");

System.out.println("Entering into prog");




[This message has been edited by Prasad Ballari (edited October 24, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Answer Must be D
sri devij
Ranch Hand

Joined: Oct 23, 2000
Posts: 37
Now I'm clear about this.Thank you all.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
'charu' and 'Murali',
Read this post for more details.
Your cooperation to comply with the official policy is appreciated.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: gc?
It's not a secret anymore!