Just to make sure we're all on the same page... I think I agree with Nick, but I can't quite follow the references.
Let's re-label the code, to be more like what's on the real exam:
Also let's not use String objects because of the confusion about the String pool. The exam won't mix GC questions together with String questions!
I like Nick's pictures
After line 3 there are two reference vars. and two objects:
Stack ......... Heap
r --------- > (SB1) // StringBuffer object 1
s --------- > (SB2)
After line four:
................... (SB1) // has no ref. var
s ----------> (SB2)
r ----------> (SB3) // new object, old ref var
After line 5:
................... (SB1) // no ref var
s ------------> (SB2)
.................... (SB3) // no ref var
r ------------> null
After line 6, no changes in ref vars because s is refering to a StringBuffer object.
After line 7 the method is popped off the stack and the reference to SB2 is also lost.
So in this example, depending on the line #, the number of objects eligible for GC is the number of objects with no reference vars!
Does that make sense?
-Bert
[ July 24, 2003: Message edited by: Bert Bates ]