Patricia Shanahan wrote:
printdude1968@gmail.com wrote:
String s = 'hello';
s += 'good-bye';
s is not a String object, it is a reference variable that can either be
null or point to a String object.
The += changes s from pointing to the "hello" String object to instead
point to a String object with value "hello"+"good-bye". It does not
change the "hello" object.
String s = "hello";
String x = s;
s += "good-bye";
Yup. To clarify just a bit more, in your original example, the original
object "hello" is tossed away. There's no reference to it, so it's just
left on the heap, and it'll be garbage-collect later. In Patricia's
example, there's a reference to "hello" retained in x, so this time the
"hello" object will stick around, and it's usable later, like for
printing out.
bring up the intern issue, and be more confusing than enlightening.
After watching this thread for a bit, I think I was right.