This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
maggie karve
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charles Chikito
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic