如何判断一个对象是同一个还是不同的
How to determine if an object is the same one or different
我知道this.toString()
打印的不是对象的地址而是散列码。我也读过像 这样的讨论
但是,假设 this.toString()
在 2 个场合打印 相同的值 和 不同的值第三次。 那,我能不能断定前两次的对象是一样的,第三次是不同的呢?
我正在尝试确定(仅用于测试)一个对象是相同的还是不同的。就像说 this
指向相同的地址或不同的地址。
在 Android 设备上检查 Java 运行 中的对象是否相同的更好方法? 或者,如果我能以某种方式获得对象的地址,那么整个难题就迎刃而解了。
如果你想检查消息的相等性,你应该使用对象 class 中可用的 equals
方法,它对 class 中的所有 class 是超级 class =23=]默认。
假设您要检查对象 Mapper
。你可以像下面那样做:
Mapper mapper = new Mapper();
Mapper mapper1 = mapper;
Mapper mapper2 = new Mapper();
mapper.equals(mapper1); //true
mapper.equals(mapper2); //false
可以有两个相同的对象 hashcode
但它们也可以不同。如果您想要一些自定义相等检查逻辑,那么您应该在 Class.
中覆盖 Object
class 的 hashcode
和 equals
方法
确定对象引用是否指向与其他引用相同的实例的最简单方法是使用 ==
运算符:
yourRef1 == yourRef2
另外,如果你想知道一个对象的不可变引用,你可以使用System.identityHashCode(yourObject)
:
Returns the same hash code for the given object as would be returned
by the default method hashCode(), whether or not the given object's
class overrides hashCode(). The hash code for the null reference is
zero.
在Java中比较两个对象主要有三种方式:
比较对象引用(您正在寻找的那个)
obj1 == obj2
当且仅当两个引用都指向同一个对象.
时,此条件为真
CustomType obj1 = new CustomType();
CustomType obj2 = ob1;
boolean thisIsTrue = obj1 == obj2;
但是:
CustomType obj1 = new CustomType();
CustomType obj2 = new CustomType();
boolean thisIsFalse = obj1 == obj2;
比较对象数据
obj1.equals(obj2)
此条件的结果取决于 equals
方法在 obj1
的 class 上的实际实施。例如。这种设计不佳的 equals
方法将始终 return 错误。即使 obj1
和 obj2
指向同一个对象。
@Override
public boolean equals(Object other){
return false;
}
CustomType obj1 = new CustomType();
CustomType obj2 = ob1;
boolean thisIsTrue = obj1 == obj2;
boolean thisIsFalse = obj1.equals(obj2);
比较对象哈希码
obj1.hashCode() == obj2.hashCode()
此条件的结果取决于 hashCode
方法在 obj1
和 obj2
的 class 上的实际实施。不鼓励使用这种方法,因为它更不可信。两个完全不同的对象可以具有相同的哈希码。在某些特定情况下应慎重使用。
我知道this.toString()
打印的不是对象的地址而是散列码。我也读过像
但是,假设 this.toString()
在 2 个场合打印 相同的值 和 不同的值第三次。 那,我能不能断定前两次的对象是一样的,第三次是不同的呢?
我正在尝试确定(仅用于测试)一个对象是相同的还是不同的。就像说 this
指向相同的地址或不同的地址。
在 Android 设备上检查 Java 运行 中的对象是否相同的更好方法? 或者,如果我能以某种方式获得对象的地址,那么整个难题就迎刃而解了。
如果你想检查消息的相等性,你应该使用对象 class 中可用的 equals
方法,它对 class 中的所有 class 是超级 class =23=]默认。
假设您要检查对象 Mapper
。你可以像下面那样做:
Mapper mapper = new Mapper();
Mapper mapper1 = mapper;
Mapper mapper2 = new Mapper();
mapper.equals(mapper1); //true
mapper.equals(mapper2); //false
可以有两个相同的对象 hashcode
但它们也可以不同。如果您想要一些自定义相等检查逻辑,那么您应该在 Class.
Object
class 的 hashcode
和 equals
方法
确定对象引用是否指向与其他引用相同的实例的最简单方法是使用 ==
运算符:
yourRef1 == yourRef2
另外,如果你想知道一个对象的不可变引用,你可以使用System.identityHashCode(yourObject)
:
Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.
在Java中比较两个对象主要有三种方式:
比较对象引用(您正在寻找的那个)
obj1 == obj2
当且仅当两个引用都指向同一个对象.
时,此条件为真CustomType obj1 = new CustomType(); CustomType obj2 = ob1; boolean thisIsTrue = obj1 == obj2;
但是:
CustomType obj1 = new CustomType(); CustomType obj2 = new CustomType(); boolean thisIsFalse = obj1 == obj2;
比较对象数据
obj1.equals(obj2)
此条件的结果取决于
equals
方法在obj1
的 class 上的实际实施。例如。这种设计不佳的equals
方法将始终 return 错误。即使obj1
和obj2
指向同一个对象。@Override public boolean equals(Object other){ return false; } CustomType obj1 = new CustomType(); CustomType obj2 = ob1; boolean thisIsTrue = obj1 == obj2; boolean thisIsFalse = obj1.equals(obj2);
比较对象哈希码
obj1.hashCode() == obj2.hashCode()
此条件的结果取决于
hashCode
方法在obj1
和obj2
的 class 上的实际实施。不鼓励使用这种方法,因为它更不可信。两个完全不同的对象可以具有相同的哈希码。在某些特定情况下应慎重使用。