• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Given the following code, how many objects will be eligible for garbage collection on

 
Anil Verghese
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}
}
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the comment //here, i think no object is eligible for garbage collection.
 
Pavan Sambare
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No object would be eligible for GC at the //here comment. This is because when the reference is passed to the findOut method, it is a copy of the original reference. So even though that copy is made to point to null, the original reference still points to the objects.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please acknowlege the source of there mock exam questions and attept to provide an answer to be discussed. Otherwise you are perceived to be lazy and taking advantage of other forum members.
 
Vivek Vikash
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that any object will be eligible for garbage collection.

Reference variables x and y are still refering to their respective objects even after the call to fn findOut(Integer aInteger) because java uses pass by value.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

how many objects are eligible for garbage collection at the comment here?





In those case of questions I use a simple trick. I always say to myself (twice):
Only objects are eligible to garbage collection.
Variables never.

OK?


But in this question...

... I'm not really sure if your answers are right. If a method ends, all local variables are eligible to GC, and the constructor is at the end at //here.
x and y are no member variables. So both the tenner and the ninety niner object should be doomed.
But I'm not sure about this. As this would be a different question, I started a seperate thread about this.


Yours,
Bu.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The tricky part here is Integer z. It is a reference variable pointed to the same Object reference variable y points to. When you null the reference z, reference y is still pointing to an Integer object on the heap. The remainder has been explained in above posts.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

no doubt about the Integer named z.

But what I'm asking myself is:
at the indicated point, the constructor is finished. No more code past this line.
So are the local variables x and y already eligible here or only after the following } ?

I think it's only a question of definition, where a block ends, at its last line or after the }


Yours,
Bu.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No internet access over weekend , hence the late reply.

I think that at the given "// here <---------" the method has yet to finish running. At that very moment, say, a billionth of a second depending on your CPU speed and JVM implementation , the objects are still referenced and therefore are not eligible for gc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic