The book is correct. When you say str = a
you are not changing anything about the String
'something'. You should distinguish between str
and something
, they are not the same. "something"
here is a String object in memory, whereas str
is just the reference to that string. Same with the reference a
.
When you say:
str = a
You are not changing something
, you are in fact saying, "change the reference str
to point to whatever the reference a
is pointing to." The String
s remain the same, the references change.
On a similar note, this is why you may see in your book that concatenating String
s is expensive, as doing something like:
str = str + a
Would again not be changing the existing String
s, but instead creating a new String
object which is equal to the concatenation of the String
that the reference str
is referring to and the String
that the reference a
is referring to.