Object的hashCode()是一个本地方法,调用的是底层的hash值,可以用来比较内存地址,因为不是一个java编写的方法,查看源码找不到它的实现。
可以复写hashCode方法,比如String类中:
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;
} 作者: 黑马11期李项京 时间: 2012-5-26 20:16
兄弟们,最后还是被我发现虚拟机的中的计算公式{:soso_e182:}int java.lang.String.hashCode()
hashCode
public int hashCode()
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]//计算公式
using int arithmetic(算法), where s[i] is the ith character of the string, n is the length of the string, and ^ indicates(表示) exponentiation(取幂). (The hash value of the empty string is zero.)
Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable