Search Tutorials


Overriding equals method in Java - Hello World Example | JavaInUse



Overriding equals() in Java

Overview

Apart from the primitives, everything in Java is an object which is extended by the Java.lang.Object. Here is the Object class methods.
Method Description
boolean equals (Object obj) Decides whether two objects are meaningfully equivalent.
void finalize() Called by garbage collector when the garbage collector sees that the object cannot be referenced.
final void notify() Wakes up a thread that is waiting for this object's lock.
final void notifyAll() Wakes up all threads that are waiting for this object's lock.
final void wait() Causes the current thread to wait until another thread calls notify() or notifyAll() on this object.
hashCode() Returns a hashcode int value for an object, so that the object can be used in Collection classes that use hashing, including Hashtable, HashMap, and HashSet.
toString() If a subclass does not override this method, it returns a "text representation" of the object.
Here equals()and other methods are defined in the Object class. So all java classes have the equals() method by default. We can override these methods in our classes.

Lets Begin

As explained before all classes extend the object class. So Employee class implicitly extends the object class as follows-

java5-5
To test if two object instances are equal we use the equals() method. It is recommended to override this method if we wish to compare the objects to test their equality based on the business requirements. If the equals method is not overridden, the default implementation of the equals method() inherited from the Object class will be used.

Default behavior of equals(If equals method is not overriden)

If the equals method is not overriden the default implementation of equals() checks if the object references of the two objects being compared are equal, that is if both the objects being compared are the exact same object. This is the same as the "==" check on two objects, which checks the value (i.e., memory address in the heap) of the two objects. Thus == returns true only if the references of the objects being compared refer to the same object instance.

Lets take a look at the different scenarios

We will use the Employee class defined below to explain the concept of overriding the equals methods. Currently we have not overridden the equals method.
package com.javainuse.domain;

public class Employee {
	private String empId;
	private String name;
	private String designation;
	private double salary;

	public Employee() {
	}

	public Employee(String empId, String name, String designation, double salary) {
		super();
		this.setEmpId(empId);
		this.name = name;
		this.designation = designation;
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

}
a. equals method not overriden and different object instances-
	Employee emp1= new Employee("E001","jack","manager",1000);
	Employee emp2= new Employee("E001","jack","manager",1000);
	
	System.out.println("Using equals for comparing emp1 and emp2 -  "+ emp1.equals(emp2));
	System.out.println("Using == for comparing emp1 and emp2 -  "+ (emp1==emp2));
Since equals method is not overridden so the default implementation of equals() checks if the object references of the two objects being compared are equal. Here Employee references emp1 and emp2 both refer different employee instances. So both equals and == return false.

java5-7
Output-

java5-6
b. equals method not overriden and same object instances-
Employee emp1= new Employee("E001","jack","manager",1000);
	Employee emp2= emp1;
	
	System.out.println("Using equals for comparing emp1 and emp2 -  "+ emp1.equals(emp2));
	System.out.println("Using == for comparing emp1 and emp2 -  "+ (emp1==emp2));
Since equals method is not overridden so the default implementation of equals() checks if the object references of the two objects being compared are equal. Here employee references emp1 and emp2 both refer same Employees instances. So both equals and == return true.

java5-8
Output-

java5-11

behavior after overriding equals method for employee class

package com.javainuse.domain;

public class Employee {
	private String empId;
	private String name;
	private String designation;
	private double salary;

	public Employee() {
	}

	public Employee(String empId, String name, String designation, double salary) {
		super();
		this.setEmpId(empId);
		this.name = name;
		this.designation = designation;
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	@Override
	public boolean equals(Object obj) {
		return ((Employee) obj).empId.equals(this.empId);
	}
}
Each employee should have a unique employee id empId. So we override the equals method such that two employee instances having the same empId should be equal. Here in overridden equals we check if empId is same for employee instances so true is returned.

c. Equals method overriden and different object instances-
Employee emp1= new Employee("E001","jack","manager",1000);
	Employee emp2= new Employee("E001","jack","manager",1000);
	
	System.out.println("Using equals for comparing emp1 and emp2 -  "+ emp1.equals(emp2));
	System.out.println("Using == for comparing emp1 and emp2 -  "+ (emp1==emp2));
Since we have overridden the equals method, the default implementation behavior of equals does not take place, it checks if the empId of employee instances are equal and so returns true. Since both emp1 and emp2 refer to different object instances == return false. So equals returns true while == returns false

java5-9
Output-

java5-12
d. Equals method overriden and same object instances-
Employee emp1= new Employee("E001","jack","manager",1000);
	Employee emp2= emp1;
	
	System.out.println("Using equals for comparing emp1 and emp2 -  "+ emp1.equals(emp2));
	System.out.println("Using == for comparing emp1 and emp2 -  "+ (emp1==emp2));
	
Since we have overridden the equals method, the default implementation behaviour of equals does not take place. Here in overridden equals we check if empId is same for employee instances so true is returned. Also both emp1 and emp2 refer to same object instances == return true. So both equals and == returns false

java5-10
Output-

java5-13

See Also

Internal working of ConcurrentHashMap in Java Image Comparison in Java Java - PermGen space vs Heap space Java - PermGen space vs MetaSpace Implement Counting Sort using Java + Performance Analysis Java 8 Features Java Miscelleneous Topics Java Basic Topics Java- Main Menu