Java immutable strings confusion

后端 未结 8 1409
粉色の甜心
粉色の甜心 2021-01-13 03:07

If Strings are immutable in Java, then how can we write as:

String s = new String();
s = s + \"abc\";
8条回答
  •  梦谈多话
    2021-01-13 03:29

    Strings are immutable.
    That means that an instance of String cannot change.

    You're changing the s variable to refer to a different (but still immutable) String instance.

提交回复
热议问题