We override equals()
method in Java to check if two objects are equal. Before overriding equals()
method in Java, first let's see when two objects are considered to be equal. Two objects are considered to be equal when they are identical (contain the same data) or in other words they are in the same state. In order to compare two objects for equality, we need to override equals()
method because it is originally defined in Object
class that takes a parameter of type Object
and compares it with this
reference. It does not make member level comparison. Following is the piece of code of equals()
method from Object
class.
public boolean equals(Object obj) { return (this == obj); }
The above implementation of equals()
will only return true
if two references point to the same object in memory because it compares memory locations with ==
operator rather than comparing contents. If two objects having same data are stored at different locations in memory then above implementation will return false
. So, when we define our own data types (classes), we need to override equals()
. Java's convention is that equals()
must be an equivalence relation. Therefore, overridden equals()
method must have the following properties:
x.equals(x)
is true
.x.equals(y)
is true
if and only if y.equals(x)
.x.equals(y)
and y.equals(z)
are true
, then so is x.equals(z)
.
In addition to the above rules, equals()
must take an Object
as argument and satisfy the following properties.
x.equals(y)
consistently return the same value, provided neither object is modified.x.equals(null)
returns false
.
By following above rules, let's implement equals()
method for the class Person
as follows:
class Person { private String fname; private String lname; private int age; public Person(String fname, String lname, int age) { this.fname = fname; this.lname = lname; this.age = age; } //Overriding equals public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Person that = (Person) obj; if (this.age != that.age) return false; if (!this.fname.equals(that.fname)) return false; if (!this.lname.equals(that.lname)) return false; return true; } } public class EqualsDemo { public static void main(String str[]) { Person p1 = new Person("Anushka", "Krishan", 7); Person p2 = new Person("Anushka", "Krishan", 7); System.out.println(p1.equals(p2)); System.out.println(p1.equals(p1)); System.out.println(p2.equals(p1)); } } OUTPUT ====== D:\JavaPrograms>javac EqualsDemo.java D:\JavaPrograms>java EqualsDemo true true true
In above piece of code class Person
has an overridden equals()
method, which took the following step-by-step approach:
true
. This test saves the work of doing all the other checks in this case.null
, return false
.false
. To determine an object's class, we use getClass()
. Note that we can use == to tell us whether two objects of type Class
are equal because getClass()
is guaranteed to return the same reference for all objects in any given class.Object
to Person
(this cast must succeed because of the previous test).false
if any instance variables do not match.
Once you have overridden equals()
, you have made your class comparable.
Hope you have enjoyed reading how to override equals method in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp