
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Guidelines for Implementing equals() Method in Java
To compare two objects, the Object class provides a method with name equals(), this method accepts an object and compares it with the current object.
If the references of these two objects are equal, then it returns true else this method returns false.
But this method returns true only if both references points to the same object. In fact, it should return true id the contents of the both objects are equal.
Example
class Employee { private String name; private int age; Employee(String name, int age){ this.name = name; this.age = age; } } public class EqualsExample { public static void main(String[] args) { Employee emp1 = new Employee("Jhon", 19); Employee emp2 = new Employee("Jhon", 19); //Comparing the two objects boolean bool = emp1.equals(emp2); System.out.println(bool); Employee emp3 = emp1; boolean bool2 = emp1.equals(emp3); System.out.println(bool2); } }
Output
false true
Overriding the equals() method
To resolve this, you can override the equals() method and compare two objects if you want but, In Java for two objects to be to equal they must be in an equivalence relation, which impels, the equals method should check for three conditions −
-
Reflexive − a=a;
When an object compared to itself using this method the result should be true.
-
Symmetric − if a=b then b=a;
The value of obj1.equals(obj2) should be equal to obj2.equals(obj1). i.e. if obj1.equals(obj2) returns true then, obj2.equals(obj1) must return true.
-
Transitive − if a=b, b=c then, a=c;;
If the value of obj1.equals(obj2) and, obj2.equals(obj3) is true then the value of obj1.equals(obj3) must be true.
While comparing an object with the current one in equals you need to check the following conditions.
- Given object should not be null.
if(obj == null ){ return false; }
- Given object should be of the current class.
if(this.getClass() != obj.getClass()){ return false; }
- The given object should be equal to the current object.
if(this == obj){ return true; }
- The instance variables of the current object should be equal to the instance variables of the given object.
Employee emp = (Employee)obj; if(this.name != emp.name|this.age != emp.age){ return false; }
Example
If you re write the above example including the above specified conditions in the equal method then it weather checks the given object is equal to the current employee object.
class Employee { private String name; private int age; Employee(String name, int age){ this.name = name; this.age = age; } public boolean equals(Object obj) { if(obj == null ){ return false; } if(this.getClass() != obj.getClass()){ return false; } if(this == obj){ return true; } Employee emp = (Employee)obj; if(this.name != emp.name||this.age != emp.age){ return false; } return true; } } public class EqualsExample { public static void main(String[] args) { Employee emp1 = new Employee("Jhon", 19); Employee emp2 = new Employee("Jhon", 19); //Comparing the two objects boolean bool = emp1.equals(emp2); System.out.println(bool); Employee emp3 = emp1; boolean bool2 = emp1.equals(emp3); System.out.println(bool2); } }
Output
true true