考虑下面的例子。
String str = new String();
str = "Hello";
System.out.println(str); //Prints Hello
str = "Help!";
System.out.println(str); //Prints Help!
在Java中,String对象是不可变的。那么为什么对象str可以被赋值为"Help!"呢?这难道不是与Java中字符串的不变性相矛盾吗?有人能给我解释一下不变性的确切概念吗?
编辑:
好的。我现在明白了,但还有一个问题。下面的代码呢:
String str = "Mississippi";
System.out.println(str); // prints Mississippi
str = str.replace("i", "!");
System.out.println(str); // prints M!ss!ss!pp!
这是否意味着将再次创建两个对象(“Mississippi”和“M!ss!ss!pp!”),并且在replace()方法之后引用str指向不同的对象?
字符串类是不可变的,你不能改变不可变对象的值。
但在String的情况下,如果你改变了String的值,它会在字符串池中创建新的字符串,而不是旧的字符串引用。通过这种方式,字符串是不可变的。
举个例子,
String str = "Mississippi";
System.out.println(str); // prints Mississippi
它将创建一个字符串“Mississippi”,并将其添加到字符串池
所以现在str指向密西西比。
str = str.replace("i", "!");
System.out.println(str); // prints M!ss!ss!pp!
但经过上述操作,
另一个字符串将被创建"M!ss!ss!pp!"
它将被添加到String池。而且
现在str指向M!ss!ss!pp!而不是密西西比州。
通过这种方式,当你改变string对象的值时,它会创建一个新的对象并将其添加到string池中。
让我们再看一个例子
String s1 = "Hello";
String s2 = "World";
String s = s1 + s2;
上面的3行代码将向字符串池中添加3个字符串对象。
1)你好
2)世界
3) HelloWorld
就像莱纳斯·托瓦兹说的:
空谈是廉价的。给我看看代码
看看这个:
public class Test{
public static void main(String[] args){
String a = "Mississippi";
String b = "Mississippi";//String immutable property (same chars sequence), then same object
String c = a.replace('i','I').replace('I','i');//This method creates a new String, then new object
String d = b.replace('i','I').replace('I','i');//At this moment we have 3 String objects, a/b, c and d
String e = a.replace('i','i');//If the arguments are the same, the object is not affected, then returns same object
System.out.println( "a==b? " + (a==b) ); // Prints true, they are pointing to the same String object
System.out.println( "a: " + a );
System.out.println( "b: " + b );
System.out.println( "c==d? " + (c==d) ); // Prints false, a new object was created on each one
System.out.println( "c: " + c ); // Even the sequence of chars are the same, the object is different
System.out.println( "d: " + d );
System.out.println( "a==e? " + (a==e) ); // Same object, immutable property
}
}
输出为
a==b? true
a: Mississippi
b: Mississippi
c==d? false
c: Mississippi
d: Mississippi
a==e? true
所以,记住两件事:
字符串是不可变的,直到你应用一个方法来操作和创建一个新的字符串(c和d的情况)。
如果两个参数相同,则Replace方法返回相同的String对象