jdk源码理解-String类

String类的理解

简记录一下对于jdk的学习,做一下记录,会持续补充,不断学习,加油

1.String的hash值的计算方法。

hash值的计算方法多种多样,jdk中String的计算方法如下,比较简单,由字符串中的字符的ASCII值计算出来。

 /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</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;
    }
原文地址:https://www.cnblogs.com/daixianjun/p/jdk-string.html