• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

gc?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I also think its C because an object is eligible for garbage collection when it refers to null.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
------------------
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Prasad is right, the answer should be D.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I choose D.
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer Must be D
 
sri devij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm clear about this.Thank you all.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic