这个就是比较值还是比较引用(C++里的指针)的判断
1、关于object的equals和==的关系,请参考:
https://www.cnblogs.com/qianguyihao/p/3929585.html
另外对于Android里的类的equals是否代表值或者引用,可以查官网,经常碰到的一种说法
The equals
method for class Object
implements the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and y
, this method returns true
if and only if x
and y
refer to the same object (x == y
has the value true
).
上述的object.equals(x)方法里是默认x非空,如果是x为空则返回false。如果考虑两个object的比较,可以使用Objects.equals(a, b)
2、关于Objects.equals()
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}