• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC doubt

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wanna try for yourself?




Yours,
Bu.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hassel, I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic