Why java String 是不可变的?

211 阅读2分钟

前言

为什String是不可变的对象。之前了解到的答案就是JVM维护了String 的常量池,不同的线程都在引用常量池中的对象如果是可变的会有线程不安全的问题。

/**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();

String#intern()方法将String对象加入常量池,这个常量池是有String的类对象维护。

String为不可变的另外一种原因

我在看数据结构看到Hash表的时候,为了提高效率会将HashCode进行缓存避免反复的计算。

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
...
/** Cache the hash code for the string */
    private int hash; // Default to 0
...
/**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }
...
}

上面计算hashCode的方法可以看出将计算结果存储到了hash这个字段。当hash为0时计算hashCode.若String可变 那么hashCode也将失效。

总结

两个原因:

  • 保证线程安全
  • 保证hashCode的计算效率