value 变量是 final + private 的:一旦被赋值,无法被更改内存地址(禁止重新赋值),同时外部无法访问内部数组进行修改。
1 2 3 4 5
publicfinalclassString implementsjava.io.Serializable, Comparable<String>, CharSequence{ /** The value is used for character storage. */ privatefinalchar value[]; }
// 1. javac Scratch.java // 源代码转化为字节码(byte code = 1111_1111), // 2. javap -v Scratch.class // The `javap` tool is used to get the information of any class or interface.
// 不可变性 >>> a = '123' >>> a[0] = '1' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> b = a >>> b += " 123 >>> b '123 123' >>> a '123' >>> a = '123' * 100000 >>> b = '123' * 100000 >>> a is b False >>> a = '123' * 10 >>> b = '123' * 10 >>> a is b True