• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • 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: 9050
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.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic