public static void main(String args[]){
//hash编码独一无二代表一个对象,通过hash编码可以找到这个对象的位置。存在特殊情况,不同对象的hash编码有可能一样
Object aobj = new Object();
Object bobj = new Object();
Object cobj = aobj;
System.out.println("aobj hashcode is "+aobj.hashCode());
System.out.println("bobj hashcode is "+bobj.hashCode());
System.out.println("cobj hashcode is "+cobj.hashCode());
String str1 = "aaa";
String str2 = "aaa";
System.out.println("str1 hashcode is "+str1.hashCode());
System.out.println("str2 hashcode is "+str2.hashCode());
}
结果是:
aobj hashcode is 1607546255
bobj hashcode is 538144878
cobj hashcode is 1607546255 和第一个相同
str1 hashcode is 96321
str2 hashcode is 96321<span style="white-space:pre"> </span> 和第四个相同