-2

I have created class A. And then I have created two objects of class A.

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

Then if i try to compare objects by using equals method, it return false.

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

In same case, if I use wrapper class like

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

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

Why? Can anyone explain?

Rose
  • 331
  • 2
  • 4
  • 8

5 Answers5

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
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 2
    I suggest also noting that, when overriding equals, it is very important to also override hashCode to ensure that two equal objects have the same hash code. – Patricia Shanahan Jul 29 '13 at 09:58
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

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2

When you have this,

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

It will return true.

Because the Integer class overrides the equals method to compare vales rather than comparing references.

For wrapper class Integer, the literals from -128 to 127 are used from the same pool.

You will find this interesting taken from HERE, see this for answer:

    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
    }
Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

You should override equals() method for class A

for example

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

}
ThiepLV
  • 1,219
  • 3
  • 10
  • 21
0

Thats because when you call equals() on A instance it calls equals of Object class, which compares references.

so if you will do:

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

To provide custom equals implementation you will have to override equals method in your class:

class A{


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

}
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120