Object的hashCode和equals方法
所属分类 java
浏览量 1094
自定义对象 不做任何重写
hashCode() 如何计算
equals() 是如何比较的
==比较的是什么
直接上代码
public class MyObject {
public static void main(String[] args) throws Exception {
MyObject obj1 = new MyObject();
System.out.println(obj1.hashCode()+","+System.identityHashCode(obj1)+","+obj1+","+Integer.toHexString(obj1.hashCode()));
MyObject obj2 = new MyObject();
MyObject obj3 = obj1;
System.out.println(obj3.hashCode()+","+System.identityHashCode(obj3)+","+obj3+","+Integer.toHexString(obj3.hashCode()));
System.out.println(obj1.equals(null));
System.out.println(obj1.equals(obj3));
System.out.println(obj1.equals(obj2));
}
}
输出结果
2018699554,2018699554,dyyx.my.MyObject@7852e922,7852e922
2018699554,2018699554,dyyx.my.MyObject@7852e922,7852e922
false
true
false
public boolean equals(Object obj) {
return (this == obj);
}
上一篇
下一篇
如何手工触发fullgc
mongodb简介
springboot2集成业务指标
面向对象 六原则 一法则
为何RedisCluster设计成16384个槽
java面试题