first
is a reference to an object of type StringBuilder
. That is, first
stores a value that can be used to refer to an object on the heap that is type of Stringuilder
. second
is another reference to an object of type StringBuilder
and its value is initially set refer to the same object that first
is referring to.
If you change the value of first
what you are doing is changing what the referent is. That is, you are using first
to refer to a different object. This does not impact second
; its value is unaffected by changes to the value of first
. (Remember: the value of first
and second
are references that initially have the same referent. But just like with
int x = 1;
int y = x;
x = 2;
does not change the value of y
, changing the value of first
does not change the value of second
.
On the other hand, when first
and second
refer to the same object, any changes to that object will be visible through both first
and second
.
Think of it like this. Let's say I create a text file first.html
whose contents are
<a href="http://stackoverflow.com">Stack Overflow</a>
and I issue the command copy first.html second.html
. Then both pages can be used to refer to the same webpage; by following the link we arrive at the same referent. If changes are made to the Stack Overflow home page, then accessing the homepage through either first.html
or second.html
will allow me to see those changes. But if I then change the contents of first.html
to be
<a href="http://www.thedailywtf.com>The Daily WTF</a>
then I can no longer use first.html
to refer to the Stack Overflow homepage. Moreover, this change does not impact the value of second.html
. It is still
<a href="http://stackoverflow.com>Stack Overflow</a>
Think of the contents of these files as the values of a reference type, and the ultimate desination as the referent object.