File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Given the following code, how many objects will be eligible for garbage collection on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Given the following code, how many objects will be eligible for garbage collection on" Watch "Given the following code, how many objects will be eligible for garbage collection on" New topic
Author

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

Anil Verghese
Ranch Hand

Joined: Oct 11, 2006
Posts: 155
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

Joined: Mar 18, 2004
Posts: 572
at the comment //here, i think no object is eligible for garbage collection.
Pavan Sambare
Greenhorn

Joined: Aug 21, 2006
Posts: 23
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.


SCJP 1.4 -- 98%<br />SCWCD 5 -- 97%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Vivek Vikash
Greenhorn

Joined: Oct 12, 2006
Posts: 8
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49


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.


EXCEL IN ALL YOU DO
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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

Joined: Oct 10, 2006
Posts: 49
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.
 
wood burning stoves
 
subject: Given the following code, how many objects will be eligible for garbage collection on
 
Similar Threads
can anyone explain this?
garbage collection
Call to function inside construction?
Garbage Collection
confused............