• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

k&b book chapter 3 -exercise 2 - clarification needed

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

so as per my understanding there should be 3 objects eligible for GC.
c1,c2 and thier associated Short object.

Please explain

thank you..
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is c2 itself actually passed into method go() or a copy of c2?

Thanks
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are passing the refrence copy of c2.
c2 is a object reference variable.
So both 'cb' and 'c2 ' wpointed to same object refernce only know.
hence when 'cb' is set to null,the 'c2' will also be 'null'

Thats the reason i think c2 will also become 'null'.

Please explain with an example.

thank you.
 
Nikos Pougounias
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, both 'cb' and 'c2' point to the same object.

So, what happens when 'cb' is set to null?

It stops pointing the object and points to null. This does not affect 'c2' at all! This does not affect the real object also!

Example: Assume object1, object2, ..., object100 of type Object that all reference the same object on the heap.


If one of them is set to null,

all the others still reference the same live object!
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cb and c3 are just references but not objects.
Lets see the stack and heap here.

stack|heap
c1 | cardboardobj1,storyobj1
c2 | cardboardobj2,storyobj2
c3 is reference variable.
Now cb ref vble refers to c2.
when cb = null, cb ref vble is made null but c2 object is still intact.
c3 ref vble is also null.
But remember ref vbles live on the stack and only objects live on the heap.
so cb and c3 are not on heap at all.
Now when c1 = null, the 2 objects associted with c1 are available for garbage collection as no references are referring to them anymore.
so 2 objects are available for GC
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic