经常被问到hashcode方法和equals方法还有== ,网上都有结论,但我们不能知其然却不知其所以然。所以我们从string的hashcode和equals入手,探究这3者,先贴源码。

 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

相关文章: