On 8/19/08, Jonah Bloch-Johnson [email protected] wrote:
I am writing because I am somewhat stumped by why the following discrepency occurs…
Variables and Array entries hold references to objects.
=> 20774910 #the same object that x referrs to
=> 21222360 #now x refers to the newly created array
=> 20774910 #but y still holds a reference to the old one.
=> 21222360 #y is now a reference to the same Array as x
=> [] #so changes to the content of that Array are visible
through y’s reference.
hope this helps,
x = 
 is an Array object, x is a variable referring to that array
y = 
y referrs to a 2nd Array
y << x
And now that array contains a reference to
x =  # (compare this)
Here you are creating an array, labeling it ‘x’, and storing a
reference to it in the array labeled y. Then you reassign the ‘x’
label to yet another array (the one containing 2).