英文:
Assigning one class to 2 variables and comparing them
问题
抱歉,如果这个问题之前已经被提出过,但我想知道:
假设我创建了一个类的新实例并将其赋值给一个变量,例如:MyClassName myclass = new MyClassName(一些参数);
,然后我将它赋值给另一个变量或者将其放入一个列表中,例如:list.add(myclass);
。如果我在一个if语句中比较这两者,if (list.get(0).equals(myclass)
,它会返回true吗?
如果问题有点模糊或不清楚,请原谅。
英文:
Sorry if this has been asked before but I wanted to know:
Say i make a new instance of a class and assign it to a variable e.g.: MyClassName myclass = new MyClassName(some parameters);
and I assign this to another variable or put it in a List e.g.: list.add(myclass);
. If I compare the two in an if clause if (list.get(0).equals(myclass)
would it return true?
Forgive me if the question might be a bit vague or unclear.
答案1
得分: 0
两种在Java中匹配对象的方法:
- 匹配一个对象与另一个对象,例如:obj1 == obj2
- 另一种方法是使用obj1.equals(obj2).... 在这种情况下,obj1必须有一个名为equals的方法...
现在,在第二种情况下会发生什么:equals
方法在大多数情况下是自定义设计的,即您必须提供它。而在某些情况下(对于内置类),它由Java提供,例如对于String
。因此,我希望您明白obj1 == obj2
不会调用equals
方法。这是因为Java没有像C++中的“运算符重载”之类的花哨功能。因此,equals
方法通常用于检查两个对象的内容是否相同。
那么,当您执行obj1 == obj2
时会发生什么?它只是检查obj1
的对象引用是否与obj2
相同。这意味着它们是否都指向相同的内存。因此,有可能obj1 == obj2
返回false,即使它们的内容相同。
让我给您展示一个示例:
class SomeClass {
private int x;
public SomeClass(int x) {
this.x = x;
}
public boolean equals(SomeClass o) {
return this.x == o.x;
}
}
现在,编写一些测试:
SomeClass a = new SomeClass(12);
SomeClass b = new SomeClass(12);
SomeClass c = a;
System.out.println(a == c); // 输出 true
System.out.println(a == b); // 输出 false
System.out.println(a.equals(c)); // 输出 true
System.out.println(a.equals(b)); // 输出 true
希望这清楚了一切。如果有任何不清楚的地方,请在评论中询问我。
英文:
Two ways of matching object in java:
- matching one object's with another object like: obj1 == obj2
- another one is obj1.equals(obj2)....in this case obj1 must have a methods called equals...
Now, what happens in the second case: equals
method is custom design in most cases, i.e. you've to provide it. And in some cases (for builtin classes), it's provided by java, for example for String
. So, I hope you understood that obj1 == obj2
does not call on the equals
method. That is because java does not have any fancy thing like operator overriding as in C++. So, the equals
method is generally used to check if two objects content are same or not.
So, what happens when you do obj1 == obj2
? It just checks if obj1
's object reference is the same or not as obj2
. That means they both target the same memory or not. So, it is possible that, obj1 == obj2
may return false even if there contents are same.
Let me show you an example:
class SomeClass {
private int x;
public SomeClass(int x) {
this.x = x;
}
public boolean equals(SomeClass o) {
return this.x == o.x;
}
}
Now, write some tests:
SomeClass a = new SomeClass(12);
SomeClass b = new SomeClass(12);
SomeClass c = a;
System.out.println(a == c); // print true
System.out.println(a == b); // print false
System.out.println(a.equals(c)); // print true
System.out.println(a.equals(b)); // print true
Hope this clears everything. If anything is unclear ask me in the comment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论