【发布时间】:2018-11-10 14:59:25
【问题描述】:
(这个问题可能是重复的,但我真的不明白其他答案)
你有以下代码:
String str ="football";
str.concat(" game");
System.out.println(str); // it prints football
但是使用这段代码:
String str ="football";
str = str + " game";
System.out.println(str); // it prints football game
那么有什么区别以及到底发生了什么?
【问题讨论】:
-
您没有将
str.concat(" game");分配给变量,因此str仍然具有其原始值。 -
字符串是不可变的,所以你必须将
str.concat(" game");赋值给一个变量