0

Suppose I have a class Employee:

class Employee{
    int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

class Example {
     public static void main(String[] args){
         Employee e1=new Employee();
         Employee e2=new Employee();
         e1.setId(1);
         e2.setId(1);
         System.out.println(e2.equals(e1));
     }
}

Why does it gives false? What is the reason for it, need a brief explanation for .equals and == method.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
Abhilash28
  • 645
  • 1
  • 9
  • 15

3 Answers3

4
  1. All classes inherit from Object
  2. Therefore they use the Object.equals method until you override it
  3. Object.equals tests for reference equality, it knows nothing about the fields in your class and cannot test for "value" equality

i.e. to test for value equality you need to override equals and provide your own implementation. As an example:

class Employee{
    int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        //see comments below for this next line
        if (o.getClass() != this.getClass()) return false;
        Employee other = (Employee)o;
        return other.id == this.id;
    }
}

Your override should satisfy the rules of reflexivity, symmetry, transitivity, consistency, and be false for a null argument, hence the complexity in the above example. To do this it does:

  • a reference check (for efficiency)
  • a null check
  • either an instanceof, or a getClass check (the choice between these two depends on your definition of equality for subtypes)
  • a cast to the same type
  • finally, the value field checks

Note also that overriding equals means you should also override hashCode:

@Override public int hashCode()
{
    return id;
}
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • I think its the == operator which checks for reference equality and equals() just checks for the objects value to be equal. – Abhilash28 Jan 17 '15 at 16:12
  • 1
    And if you override .equals, you should also override .hashcode. – Brett Okken Jan 17 '15 at 16:13
  • @abhilash No. In `Object`, as defined, `equals` does a reference equality check. – Andy Brown Jan 17 '15 at 16:17
  • @BrettOkken Good spot, I think you were typing that as I was adding that in. – Andy Brown Jan 17 '15 at 16:17
  • @abhilash - Object.equals() is implemented as (this == o), which is what he's referring to. .equals() in general can check values of fields in the object, values of some fields in the object, reference, a look-up in some table, or whatever else you can imagine that holds to the contract of equals. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Object.java#Object.equals%28java.lang.Object%29 – James Jan 17 '15 at 16:18
1

== will compare if two objects are the same reference, so from your example:

e1 == e1; // true
e1 == e2; // false

equals checks if two objects are logically equal. The default implementation will do just as == does, therefore you need to provide your own implementation. For your example a very trivial implementation would be:

 @Override
 public boolean equals(Object o) {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;

     Employee other = (Employee) o;

     return id == other.id;
 }
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
-1

About .equals() and == you can read this thread to find more :-) About your question, and as said above, you need to override .equals() method in class Example (both of them) the reason for that is because in Java as you may already know, almost everything is subclass of Object therefore it uses the methods of Object and thats the reason why .equals() method apears even if its not implemented in your code. To override the .equals() either do it manually or if you use eclipse, go to Source>Generate hashCode() and equals()... and when the 2 methods are overwritten in your code, you can edit them in a way they suit your code :)

Hope i helped!

Community
  • 1
  • 1
fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24