identityHashCode()起始就是缺省的hashCode实现,不管hashCode是否被overrideString类中的hashCode实现: // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
String类中hashCode(),equals()都被改写了String s1 =new String("hhh")
String s2 =new String("hhh")因为s1.equals(s2) return true,所以s1.hashCode() 必须== s2.hashCode()
只有同一个对象才会相等!!
identityHashCode()起始就是缺省的hashCode实现,不管hashCode是否被overrideString类中的hashCode实现:
// s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count; for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}