Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

GC doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wanna try for yourself?




Yours,
Bu.
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hassel, I got it.
 
We noticed he had no friends. So we gave him this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic