| Author |
GC doubt
|
dolly shah
Ranch Hand
Joined: Jun 18, 2007
Posts: 383
|
|
Object a = new Object(); 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?i think only one object but answer is 3 objects. GC related questions are too tricky. I always give wrong answer. Can anyone please clear my doubt?
|
SCJP-1.5<br />SCWCD-1.4
|
 |
V Gala
Ranch Hand
Joined: Aug 06, 2007
Posts: 113
|
|
Object a = new Object(); //object 1 Object b = new Object(); //object 2 Object c = new Object(); //object 3 Object d = new Object(); //object 4 d=c=b=a; d=null; d=c=b=a; after this statement a,b,c and d all are pointing to a object 1 since a value has been assign to b,c and d now there are no variable pointing to object 2,object 3 and object 4 d has been assign to null byt a is still pointing to object 1
|
 |
Onkar Joshi
Ranch Hand
Joined: Mar 01, 2007
Posts: 116
|
|
Originally posted by dolly shah: Object a = new Object(); 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?i think only one object but answer is 3 objects. GC related questions are too tricky. I always give wrong answer. Can anyone please clear my doubt?
When "d=c=b=a" is executed, d,c,b all take the value of a and the Objects they earlier referred to become eligible for garbage collection. "d=null" does not make any difference since c,b and a will still refer to the object originally referred to only by a.
|
SCJP 5 - 95% | SCWCD 1.4 - 88% | SCBCD 5 - 93%
Onkar Joshi's blog | LinkedIn profile
|
 |
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
|
|
Wanna try for yourself? Yours, Bu.
|
all events occur in real time
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8717
|
|
fyi, you probably won't see a line of code like this: a = b = c = d; on the real exam - but it's still interesting to discuss how this line of code works.
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
dolly shah
Ranch Hand
Joined: Jun 18, 2007
Posts: 383
|
|
|
Thanks Hassel, I got it.
|
 |
 |
|
|
subject: GC doubt
|
|
|