aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes k&b book chapter 3 -exercise 2 - clarification needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "k&b book chapter 3 -exercise 2 - clarification needed" Watch "k&b book chapter 3 -exercise 2 - clarification needed" New topic
Author

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

m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
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..
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110


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

Thanks


Nikos' Java blog
m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
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

Joined: Jan 16, 2008
Posts: 110
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!
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
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


If you worry you cannot work... If you work you need not worry
m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: k&b book chapter 3 -exercise 2 - clarification needed