//hashCode()
//返回该对象的哈希码值。
结果
96354
96354
这样说来s1 和s 是同一对象
但new 的应该是2个不同对象
不知道我那里在错了 请指正!
public class HashcodeDemo
{
public static void main(String args[])
{
String s = new String ("abc");
String s1 = new String ("abc");
System.out.println(s.hashCode());
System.out.println(s1.hashCode());
System.out.println(s==s1);
}
}
所以equals是一样
string的equals是比较内容
我有看到
能推出 equals 为真 hashCode一定为真
能推出 hashCode 为真 但equals不一定为真
最好用简单的实例证明!
hascode相同只表示equal相等,与==没关系的
打错,equal 与 == 是不一样的
==是比较内存地址,不受人为影响。equals和hashCode都是自定义规则,想怎么算就怎么算!
唯一能确定的就是,如果o1==o2是true,那o1.equals(o2)和o1.hashCode()==o2.hashCode()一定是true。
private static long id = 0;
public int hashCode() {
return id++;
} public boolean equals(Object o) [
return false;
}
}
如果a.hashCode() == b.hashCode(),则不一定有:a.equals(b) == true