Originally posted by Fred Roger: Question is: What will happen to the refernce named i1.other, i2 other. Will they also refered to 3 now ?? Or they will continue to refering to theie old objects ??
The latter (second scenario).
According to second scenerio there will be no objects GC at // here.
Both 1 & 2 will be eligible for GC after "// here" -- an object is eligible for garbage collection if it cannot be accessed by any live thread (and that can be the case even if an object still has references pointing at it). This is the situations that K&B call "islands of isolation", if you have that book; see page 249. [ November 07, 2006: Message edited by: Matt Russell ]
1 ----> referred by i1 and i2.other 2 ----> referred by i2 and i1.other 3 ----> referred by i3 and i3.other
After line 1:
As i1 and i2 will be refering to 3(3rd object) 3-----> i1, i2, i3, i3.other
Question is: What will happen to the refernce named i1.other, i2 other.
Will they also refered to 3 now ??
It may help your understanding if you avoid thinking of the reference variables as i1.other and i2.other. We have six reference variables: three nice easy-to-understand reference variables i1, i2 and i3 on the stack and three more sitting inside the three objects 1, 2 and 3 on the heap. So it might be better to think of these three as being 1.other, 2.other and 3.other, because the references they hold don't change when the references that i1, i2 and i3 hold get changed.
Once i1 and i2 have been made to point to object 3, the reference variable 1.other is pointing to object 2 and the reference variable 2.other is pointing to object 1. But since there are no stack variables pointing to either object 1 or object 2 they have become unreachable Islands of Isolation and so they are eligible for garbage collection.
Originally posted by Sanjeev Kumar Singh: Nice explanation Bu. One more question? When // doStuff is reached, how many objects are eligible for GC?
2 objects: c1 and c3. Although obj c2 is passed to the go method and set it's value to null, it will not change the actual object since c2 is just a copy of the reference.
Joined: Aug 25, 2006
wasn't this CardBoard thing in the K&B book?
the line CardBoard c3 = c1.go(c2);
does nothing more than Card board c3 = null;
because you cannot null a reference via a method parameter. So basically we have CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = null; c1 = null; When we are here two objects are eligible, the first CardBoard and its Short that is an Object through autoboxing.
--- ticker: ... democrats took lancashire, la mancha, minas gerais and rajastan.... gop defends tasmania ...
Joined: Nov 05, 2006
Question: Short x = 5; is created on heap? I've heard something about some sort of "pool" if numbers are between -128 +127. Example: Integer i1 = 10, Integer i2 = 10