ANKIT SRIVASTAVA
ANKIT SRIVASTAVA

Reputation: 173

equals giving false even though state of object is same in java

My code looks somewhat like this :

public class EqualityTrial {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    EqualityTrail1 obj1 = new EqualityTrail1();
    obj1.setName("Ankit");
    obj1.setAge(23);
    EqualityTrail1 obj2 = new EqualityTrail1();
    obj2.setName("Ankit");
    obj2.setAge(23);

    if(obj1 == obj2){
        System.out.println("== gave true");
    }else{
        System.out.println("== gave false");
    }
    if(obj1.equals(obj2)){
        System.out.println("equals gave true");
    }else{
        System.out.println("equals gave false");
    }

  }

}

every time both the conditions is giving false. I know == compares if reference is pointing to same object which in this case is not so false is a correct result but in case of 'equals' method as far as i know it compares state of both the objects...If the state is same method should return true which in this case is returning false even though state of both the objects is same. AM i missing something here ?

Upvotes: 0

Views: 2108

Answers (5)

Liebertee
Liebertee

Reputation: 88

If you want the equals to work property, you will have to implement an own variant of equals(Object obj).

There you can specify how the 'measuring' of equalness should be applied to the object.

Some example (used Eclipse, Source tab):

public class MyClass {

  String sequence;

  public MyClass(String seq) {
    this.sequence = seq;
  }

  @
  Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((sequence == null) ? 0 : sequence.hashCode());
    return result;
  }

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

Upvotes: 1

ANKIT SRIVASTAVA
ANKIT SRIVASTAVA

Reputation: 173

I had not overwritten the equals method....after doing the same it worked thanks.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234715

Unless you provide an implementation for equals, which should compare each field in turn, Java will use the default version in java.lang.Object which, according to the JLS:

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).

So, since you have two distinct objects, they will not compare equal by default.

Upvotes: 0

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Have you rewritten the method equals of class EqualityTrail1 ? If not this is the problem. If yes post your equals method

Upvotes: 0

Eran
Eran

Reputation: 393851

equals compares state only if you override it in your EqualityTrail1 class with an implementation that compares state. If you don't override it, the default implementation (from Object class) is used, and that implementation compares the references (so it will behave exactly the same as ==).

A possible implementation:

public class EqualityTrail1 
{
    ...
    @Override
    public boolean equals (Object other)
    {
         if (!(other instanceof EqualityTrail1))
             return false;
         EqualityTrail1 oet = (EqualityTrail1) other;
         return this.name.equals(oet.name) && this.age == oet.age;
    }

}

Upvotes: 5

Related Questions