-2

我已经创建了类A。然后我创建了两个 class 对象A

A a1=new A(5);
A a2=new A(5);

然后,如果我尝试使用equals方法比较对象,它会返回false.

if (a1.equals(a2)) // ->false

在同样的情况下,如果我使用包装类

Integer i1=new Integer(5);
Integer i2=new Interger(5);

if (i1.equals(i2)) // ->returns true

为什么?谁能解释一下?

4

5 回答 5

6

You have to override the equals() method in your class A. By default it uses the implementation inherited from Object class , i.e. Object#equals() which compares references, i.e. if both the objects are the same object residing in the heap , it returns true or else false. If you need to check for equality of contents of your objects override the equals() method in your class.

Note: It is generally a good practice to override hashCode() while overidding equals().

I have provided a sample implementation :

class A {
 private int value ;
 A(int value) {
     this.value = value;
 }
 @Override
 public boolean equals(Object obj) {
    if(obj==null || !(obj instanceof A)){
        return false;
    }
    return this.value==((A)obj).value; 
 }
 @Override
 public int hashCode() {
     return this.value;
 }
}

Integer class already overrides this method , Integer#equals().

Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

Suggested Reading:

  1. Overriding equals and hashCode in Java.
  2. Joshua Bloch - always override hashCode when you override equals
于 2013-07-29T09:53:47.783 回答
2

For that you need to override equals() method for class A If you do not override the method it will use the Object class equals method which checks whether the reference varaibles refer to the same object or not.

With Integer class, the equals() method is already overridden and which checks for the value.

Also, whenever you are overriding equals(), override hashcode() also

public class A {

    private int number;

    A(int number){
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + number;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        A other = (A) obj;
        if (number != other.number)
            return false;
        return true;
    }

    public static void main(String[] args) {
        A a1 = new A(5);
        A a2 = new A(5);

        System.out.println(a1.equals(a2));
    }


}

This prints out true.

Why? Can anyone explain?

With Integer class, the equals() method is already overridden and which checks for the value.

Integer#equals()

A good tutorial helping understand the significance of equals method is provided on theJavaGeek

于 2013-07-29T09:53:38.630 回答
2

当你有了这个,

 Integer i1=new Integer(5);
 Integer i2=new Interger(5);
 if(i1.equals(i2))

它会返回true

因为Integer该类重写了equals比较值而不是比较引用的方法。

对于包装类 Integer,从同一池中使用从 -128 到 127 的文字。

你会发现这个有趣的取自这里,请看这个答案

    Integer i1 = 127;
    Integer i2 = 127;

    if(i1==i2){
        System.out.println("true"); //prints true
    }else{
        System.out.println("false");
    }

    Integer i3 = 128;
    Integer i4 = 128;

    if(i3==i4){
        System.out.println("true");
    }else{
        System.out.println("false"); //prints false
    }
于 2013-07-29T09:54:58.460 回答
0

那是因为当您调用实例时,它会调用 equals of equals()class ,它比较引用AObject

所以如果你这样做:

instance1.equals(instance1); //will return true

要提供自定义 equals 实现,您必须在类中覆盖 equals 方法

class A{


   @Override
   public boolean equals(Object obj){
      return equality;
   }

}
于 2013-07-29T09:55:48.130 回答
0

您应该覆盖 A 类的 equals() 方法

例如

class A{
   int b;
   A(int b){
      this.b = b;
   }
   @Override
   public boolean equals(A a){
      return this.b ===a.b;      
   }

}
于 2013-07-29T09:55:00.627 回答