[英]this in equals method
I am looking at the equals method, and I see this
and I don't understand what it means...I do understand them when I see it in constructors and some methods but its not clear to me when they are in equals method something like this: 我正在查看equals方法, this
我不明白这意味着什么...当我在构造函数和某些方法中看到它时,我确实理解它们,但是当它们在equals方法中时,我不清楚像这样:
(obj == this)
...what does this mean here ? (obj == this)
...这是什么意思? where does it come from ? 它从何而来 ?
I understand when it says something like this.name = name; 我明白当它说这样的话。
from a method like this 从这样的方法
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
sorry it might be a duplicate but I couldnt find anything... 抱歉,这可能是重复的,但我找不到任何东西...
this
is the current Object instance. this
是当前的Object实例。 Whenever you have a non-static method, it can only be called on an instance of your object. 只要有非静态方法,就只能在对象的实例上调用它。
You have to look how this is called: 您必须查看如何称呼它:
someObject.equals(someOtherObj);
This invokes the equals
method on the instance of someObject
. 这将在someObject
实例上调用equals
方法。 Now, inside that method: 现在,在该方法内部:
public boolean equals(Object obj) {
if (obj == this) { //is someObject equal to obj, which in this case is someOtherObj?
return true;//If so, these are the same objects, and return true
}
You can see that this
is referring to the instance of the object that equals is called on. 您可以看到this
是指调用equals的对象的实例。 Note that equals()
is non-static, and so must be called only on objects that have been instantiated. 请注意, equals()
是非静态的,因此必须仅在已实例化的对象上调用equals()
。
Note that ==
is only checking to see if there is referential equality; 注意, ==
仅用于检查是否存在引用相等。 that is, the reference of this
and obj
are pointing to the same place in memory. 也就是说, this
和obj
的引用指向内存中的同一位置。 Such references are naturally equal: 这样的引用自然是相等的:
Object a = new Object();
Object b = a; //sets the reference to b to point to the same place as a
Object c = a; //same with c
b.equals(c);//true, because everything is pointing to the same place
Further note that equals()
is generally used to also determine value equality. 还要注意, equals()
通常也用于确定值相等。 Thus, even if the object references are pointing to different places, it will check the internals to determine if those objects are the same: 因此,即使对象引用指向不同的位置,它也会检查内部结构以确定那些对象是否相同:
FancyNumber a = new FancyNumber(2);//Internally, I set a field to 2
FancyNumber b = new FancyNumber(2);//Internally, I set a field to 2
a.equals(b);//true, because we define two FancyNumber objects to be equal if their internal field is set to the same thing.
You are comparing two objects for equality. 您正在比较两个对象是否相等。 The snippet: 片段:
if (obj == this) {
return true;
}
is a quick test that can be read 是可以阅读的快速测试
"If the object I'm comparing myself to is me, return true" “如果我要比较的对象是我,请返回true”
. 。 You usually see this happen in equals
methods so they can exit early and avoid other costly comparisons. 您通常会在equals
方法中看到这种情况,因此它们可以提早退出并避免其他昂贵的比较。
this
refers to the current instance of the class (object) your equals-method belongs to. this
是指您的equals-method所属的类(对象)的当前实例 。 When you test this
against an object, the testing method (which is equals(Object obj)
in your case) will check wether or not the object is equal to the current instance (referred to as this
). 当您针对某个对象测试this
对象时,测试方法(在您的情况下为equals(Object obj)
)将检查该对象是否等于当前实例(称为this
)。
An example: 一个例子:
Object obj = this;
this.equals(obj); //true
Object obj = this;
new Object().equals(obj); //false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.