字符串不可变的条件
在String源码中有如下代码:
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
这就导致了每次生成的字符串不可变动。每次变动都是生成新的数组;再来一次深拷贝;
字符串不可变的条件
在String源码中有如下代码:
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
这就导致了每次生成的字符串不可变动。每次变动都是生成新的数组;再来一次深拷贝;