aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
public class Test{
public static void main(String[] args) {
Object a = new Object(); // the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();

d=c=b=a;
d=null;
}
}

How many objects are eligible for GC in the following code after d = null?

1. 1
2. 2
3. 3
4. 4

here answer is 3..m getting really confused....
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
before d=null.....d only refers to be object referred by a(which now refers to b)...so when d is made null...only one object is eligible for gc..that is the first one you have made....


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
There're 4 objects created. Line "d=c=b=a" assigns all 4 references to the first created object (initially referenced by a), making 3 other object eligible for GC. Last assignment "d = null" doesn't matter.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
sorry made a wrong diagram....all the obbjects refer to the first diagram...so 3 are eligible for gc..right....oooops
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
assignment operator is right associative. so start working from right on this statement:



when b=a executes, bits in a is copied in b, means now both a and b refers to the same object...so object referred by b previously is eligible for GC.
similarly, for c=b, b and c refers to same object. Means now a,b, and c refer to same object.... so now object referred by c previously is eligible for GC.
again, d=c, so a,b,c and d refer to same object, and object referred by d previously is eligible for GC.

no change on object referred by a.
So total 3.

Hope this helps.


SCJP 5.0 100%
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
There're 4 objects created. Line "d=c=b=a" assigns all 4 references to the first created object (initially referenced by a), making 3 other object eligible for GC. Last assignment "d = null" doesn't matter.


Yes. This is how it works. Assignment operator must be evaluated from R->L

_charles
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Curve please Quote Your Sources when you post a question. This is the 4th time you've asked a mock exam question without quoting the source. Quoting the source is not optional, and if you don't quote your source properly, your question might be deleted...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection