【发布时间】:2021-08-24 05:01:18
【问题描述】:
当两个引用变量引用具有相同哈希码值的同一个对象时,== 返回 false 背后的逻辑是什么?
public class One {
public static void main(String[] args) {
One o = new One();
One o1 = o;
System.out.println(o.toString());
System.out.println(o1.toString());
System.out.println(o.hashCode());
System.out.println(o1.hashCode());
// Why does it print false ?
System.out.println(o.toString()==o1.toString()); // false
System.out.println(o.hashCode()==o1.hashCode()); // true
System.out.println(o.equals(o1)); // true
System.out.println(o.toString().hashCode()==o.toString().hashCode()); // true
}
}
【问题讨论】:
标签: java equals tostring hashcode object-class