• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

gc?

 
sri devij
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. }
8.}
When is the Integer object, created in line 3, eligible for garbage collection?
A.never
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?
 
charu
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I also think its C because an object is eligible for garbage collection when it refers to null.
 
Prasad Ballari
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards
Prasad.
------------------
 
Sudhir Bangera
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Prasad is right, the answer should be D.
 
Peter Wei Cai
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I choose D.
 
Prasad Ballari
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

myRef.getValue(c);



}
}

Prasad.
------------------

[This message has been edited by Prasad Ballari (edited October 24, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Answer Must be D
 
sri devij
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm clear about this.Thank you all.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'charu' and 'Murali',
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Your cooperation to comply with the official policy is appreciated.
Ajith
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic