不可变的简单含义是不可修改或不可改变。 在 Java 中,String
是不可变的,即一旦创建了字符串对象,它的值就不能改变。 请参考以下示例以更好地理解。
class Testimmutablestring {
public static void main(String args[]) {
String s = "Yii";
s.concat(" bai.com");//concat() method appends the string at the end
System.out.println(s);//will print Yii because strings are immutable objects
}
}
运行结果是:
Yii