In your example, a
refers first to "a"
, and then to "ty"
. You're not mutating any String
instance; you're just changing which String
instance a
refers to. For example, this:
String a = "a";
String b = a; // b refers to the same String as a
a = "b"; // a now refers to a different instance
System.out.println(b);
prints "a", because we never mutate the String
instance that b
points to.