我在不可变字符串上写了下面的代码。

public class ImmutableStrings {

    public static void main(String[] args) {
        testmethod();
    }

    private static void testmethod() {
        String a = "a";
        System.out.println("a 1-->" + a);
        a = "ty";
        System.out.println("a 2-->" + a);
    }
}

输出:

a 1-->a  
a 2-->ty

这里变量a的值被改变了(尽管许多人说不可变对象的内容不能被改变)。但是说字符串是不可变的到底是什么意思呢?你能给我解释一下这个话题吗?

来源:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html


当前回答

If some object bar holds a reference to a mutable object foo and encapsulates some of its state in mutable aspects of foo's state, that will allow code which can change those aspects of foo to change the corresponding aspects of bar's state without actually touching bar or even knowing of its existence. Generally, this means that objects which encapsulate their own state using mutable objects must ensure that no references to those objects are exposed to any code which might unexpectedly mutate them. By contrast, if bar holds a reference to an object moo and only uses immutable aspects of moo other than identity to encapsulate its state, then bar can freely expose moo to outside code without worrying about anything the outside code might do to it.

其他回答

可能上面提供的每个答案都是正确的,但我的答案是特定于使用hashCode()方法,以证明诸如,字符串…一旦创建就不能修改,修改后会在不同的内存位置产生新的值。

public class ImmutabilityTest {

    private String changingRef = "TEST_STRING";

    public static void main(String a[]) {

        ImmutabilityTest dn = new ImmutabilityTest();

        System.out.println("ChangingRef for TEST_STRING OLD : "
                + dn.changingRef.hashCode());

        dn.changingRef = "NEW_TEST_STRING";
        System.out.println("ChangingRef for NEW_TEST_STRING : "
                + dn.changingRef.hashCode());

        dn.changingRef = "TEST_STRING";
        System.out.println("ChangingRef for TEST_STRING BACK : "
                + dn.changingRef.hashCode());

        dn.changingRef = "NEW_TEST_STRING";
        System.out.println("ChangingRef for NEW_TEST_STRING BACK : "
                + dn.changingRef.hashCode());

        String str = new String("STRING1");
        System.out.println("String Class STRING1 : " + str.hashCode());

        str = new String("STRING2");
        System.out.println("String Class STRING2 : " + str.hashCode());

        str = new String("STRING1");
        System.out.println("String Class STRING1 BACK : " + str.hashCode());

        str = new String("STRING2");
        System.out.println("String Class STRING2 BACK : " + str.hashCode());

    }
}

输出

ChangingRef for TEST_STRING OLD : 247540830
ChangingRef for NEW_TEST_STRING : 970356767
ChangingRef for TEST_STRING BACK : 247540830
ChangingRef for NEW_TEST_STRING BACK : 970356767
String Class STRING1 : -1163776448
String Class STRING2 : -1163776447
String Class STRING1 BACK : -1163776448
String Class STRING2 BACK : -1163776447

在你的例子中,变量a只是一个字符串对象实例的引用。当你说a = "ty"时,你实际上并没有改变字符串对象,而是将引用指向string类的一个完全不同的实例。

Java字符串是不可变的,字符串将以对象的形式存储值。所以如果你赋值String a="a";它将创建一个对象,值存储在其中,如果你分配值a="ty"意味着它将创建另一个对象存储在其中的值,如果你想清楚地理解,检查字符串的has代码。

String S1="abc";
S1.concat("xyz");
System.out.println("S1 is", + S1);
String S2=S1.concat("def");
System.out.println("S2 is", + S2);

这表明一旦创建了一个字符串对象,它就不能被更改。每次你需要创建new并放入另一个String。年代

字符串是一个char[],包含一系列UTF-16代码单元,该数组的int偏移量和int长度。

为例。

String s

它为字符串引用创建空间。赋值复制周围的引用,但不修改这些引用所引用的对象。

你也应该意识到

new String(s)

并没有做任何有用的事情。它只是创建了另一个实例,支持的数组、偏移量和长度与s相同。很少有理由这样做,因此大多数Java程序员认为这是一种糟糕的做法。

Java双引号字符串,如“my string”,实际上是对内部string实例的引用,因此“bar”是对同一个string实例的引用,而不管它在代码中出现了多少次。


“hello”创建了一个被池化的实例,而新的String(…)创建了一个非池化的实例。尝试system . out。println((“hello”= =“你好 ") + "," + ( 新的字符串“hello”= =“你好 ") + "," + ( 新的字符串(“hello”)= =新的字符串(“hello”)));你应该看到真,假,假