What does immutable mean?

后端 未结 9 741
借酒劲吻你
借酒劲吻你 2020-11-29 16:07

If a string is immutable, does that mean that.... (let\'s assume JavaScript)

var str = \'foo\';

alert(str.substr(1)); // oo

alert(str); // foo
9条回答
  •  无人及你
    2020-11-29 16:24

    I'm not certain about JavaScript, but in Java, strings take an additional step to immutability, with the "String Constant Pool". Strings can be constructed with string literals ("foo") or with a String class constructor. Strings constructed with string literals are a part of the String Constant Pool, and the same string literal will always be the same memory address from the pool.

    Example:

        String lit1 = "foo";
        String lit2 = "foo";
        String cons = new String("foo");
    
        System.out.println(lit1 == lit2);      // true
        System.out.println(lit1 == cons);      // false
    
        System.out.println(lit1.equals(cons)); // true
    

    In the above, both lit1 and lit2 are constructed using the same string literal, so they're pointing at the same memory address; lit1 == lit2 results in true, because they are exactly the same object.

    However, cons is constructed using the class constructor. Although the parameter is the same string constant, the constructor allocates new memory for cons, meaning cons is not the same object as lit1 and lit2, despite containing the same data.

    Of course, since the three strings all contain the same character data, using the equals method will return true.

    (Both types of string construction are immutable, of course)

提交回复
热议问题