(这个问题可能是重复的,但我真的不明白其他答案(
您有以下代码:
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 + " game";
具有相同的含义。如果您不将结果分配回某个位置,它将丢失。您需要做:
str = str.concat(" game");
'concat' 函数是不可变的,因此它的结果必须放在一个变量中。用:
str = str.concat(" game");