How does equals() method work in Java
Points To Remember
  • equals(), method is a method of the Object class, hence is available in all classes.
  • Syntax of equals in Object class is public boolean equals(Object obj).
  • It return true if two objects are same, in object class it just checks if the two references points to the same class. 
  • We can override the equals method to provide custom logic to say the two objects are same. See How to override equals method
Concept: Making two objects same.
Suppose we have a Book class and each object represents a book. We create three objects book1, book2 and book3 (new keyword always creates a new object) and the objects book1, book2 refer to same book same with name 'Think Think' and id '1', book 3 refers to book 'Bad Kitty'. So in real case scenario the book1 and book2 objects should be same or equal.
id=1, name = Think Think 
id=2, name= Bad Kitty












So lets check how many objects are created and if they are equal when their member variables are same. (According to our case, objects with same id and name should be equal)
class Book{
 int id;
 String name;
 public Book(int id, String name){
   this.id = id;
   this.name= name;
 }
}

class Test{
 public static void main(String args[]){
  Book book1 = new Book(1,"Think Think");
  Book book2 = new Book(1,"Think Think");
  Book book3 = new Book(2,"Bad Kitty");

  System.out.println("book1.equals(book2) = "+ book1.equals(book2));
  System.out.println("book1.equals(book3) = "+ book1.equals(book3));
  System.out.println("book2.equals(book3) = "+ book2.equals(book3));
 }
}
book1.equals(book2) = false
book1.equals(book3) = false
book2.equals(book3) = true
The above output proves that there are only 2 objects created, one for book1, and other for book2, book3 shares the object of book2.

But we may want to make objects book1 and book2 as same since they represents the same book (they have same book id and book name ). So we need to do some thing to make all the book objects that have same id and name as same objects or equal objects.

What we need to do is, override equals and hashcode methods of the Object class. The following program shows how we can say the two objects with same id and name are equal.
class Book{
 int id;
 String name; 

 public Book(int id, String name){
   this.id = id;
   this.name= name;
 }

 @Override
 public boolean equals(Object obj){
  // Null checks not included.
  Book book = (Book)obj;   
  if(this.name.compareTo(book.name)==0 && this.id == book.id)
    return true;
  else
    return false;
 }

 @Override
 public int hashCode(){
   return this.name.hashCode() * this.id;
 }
}

class Test{
 public static void main(String args[]){
  Book book1 = new Book(1,"Think Think");
  Book book2 = new Book(1,"Think Think");
  Book book3 = new Book(2,"Bad Kitty");
  System.out.println("book1.equals(book2) = "+ book1.equals(book2));
  System.out.println("book1.equals(book3) = "+ book1.equals(book3));
  System.out.println("book2.equals(book3) = "+ book2.equals(book3));
 }
}
book1.equals(book2) = true
book1.equals(book3) = false
book2.equals(book3) = false
The objects with reference book1 and book2 are equal since according to our custom logic, book object with same id and name must be equal. Also do read the contract between hashCode() and equals() method.

No comments :

Post a Comment