There is question in K&B 1.5 Study Guide which answer I can`t understand:
Let`s take the code snippet:
When // doStuff is reached, how many objects are eligible for GC?
Answer is: 2 objects while i`m sure it is: 1 object.
Why? 5<127, so both c1.story and c2.story references point to the same object (of Short class) - i mean in the code above there is only one Short object created. So, when c1 becomes null there is only single object eligable for garbage collection (first of 2 CardBoard objects).
I think that c1 and c2 are referring to different object.
c1 ---> new CardBoard(); c2 ---> new CardBoard();
Can you tell me why c1 and c2 are referring to the same object? According to my opinion, only the object referred by c1 can be released. the one referred by c2 cannot be released. So, the answer is 1 object.
Joined: Oct 19, 2005
I didn`t say that c1 and c2 point to the same object but c1.story and c2.story:
c1.story --> new Short(5) <-- c2.story
Check K&B SCJP 1.5 Study Guide - page 235/236 for the reason of that.
You are Right. There is only one object eligible to GC and that is c1.
Joined: Aug 28, 2006
So it means that for the above code when Short story = 5 , only one object c1 is eligible for GC . Please Ranchers correct me if i am wrong
Also please explain the below statement by example if possible ; When Short story = 5, you end up with all the objects referencing the pooled instance of the Short Object. But when its made as Short story = 200, there will be no pooled instance created, as the value is out of range(-128 to 127). So new Short objects are created for each instance of the class.
When Short story = 5, you end up with all the objects referencing the pooled instance of the Short Object. But when its made as Short story = 200, there will be no pooled instance created, as the value is out of range(-128 to 127). So new Short objects are created for each instance of the class.
Is this applies in scjp1.4? I think for scjp1.4,2 objects eligible for GC.one is c1 and one is short object that is associated with C1.