What is the use of equals method in Java?

Equals method is used when we compare two objects. Default implementation of equals method is
defined in Object class. The implementation is similar to == operator. Two object references are equal
only if they are pointing to the same object.
Consider the following example

class Student {
	private int marks;

	public int getMarks() {
		return marks;
	}

	public void setMarks(int var) {
		this.marks = var;
	}
}

public class TestClass {

	public static void main(String[] args) {
		Student john = new Student();
		john.setMarks(10);

		Student kevin = new Student();
		kevin.setMarks(10);
		
		//Student peter = kevin;

		if (john.equals(kevin))
			System.out.println("Both objects are equal, compare by default equals method");
		else if (john == kevin)
			System.out.println("Both objects are equal, compare by == ");
		else
			System.out.println("Not equal");
	}
}

In the above example, marks for john and kevin are same but the output will be “Not equal” because here we are comparing only objects instead of comparing their marks. Neither equals nor == are comparing the object values. Now if we modify our code to this :

class Student {
	private int marks;

	public int getMarks() {
		return marks;
	}

	public void setMarks(int var) {
		this.marks = var;
	}
}

public class TestClass {

	public static void main(String[] args) {
		Student john = new Student();
		john.setMarks(10);

		Student kevin = john;
		if (john.equals(kevin))
			System.out.println("Both objects are equal, compare by default equals method");

		if (john == kevin)
			System.out.println("Both objects are equal, compare by == ");
		
	}
}

Output of this code is :

Both objects are equal, compare by default equals method
Both objects are equal, compare by == 

Here both the objects are pointing to each other, so the output will be calculated equal by == and equals as well, but the basic question still remains is “how to compare values within objects”. For achieving this we have to override equals method.

Example

package interviewquestions;

class Student {
	private int marks;

	public int getMarks() {
		return marks;
	}

	public void setMarks(int var) {
		this.marks = var;
	}

	// Overriding equals method
	@Override
	public boolean equals(Object obj) {
		Student st = (Student) obj;
		if (st.getMarks() != this.marks)
			return false;
		return true;
	}
}

public class TestClass {

	public static void main(String[] args) {
		Student john = new Student();
		john.setMarks(10);

		Student kevin = new Student();
		kevin.setMarks(10);

		if (john.equals(kevin))
			System.out.println("Marks are same for both students");
		else
			System.out.println("Marks are not same for both students");
	}
}

Output

Marks are same for both students

Any equals implementation should satisfy these properties:

  1. Reflexive. For any reference value x, x.equals(x) returns true.
  2. Symmetric. For any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  3. Transitive. For any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
  4. Consistent. For any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, if no information used in equals is modified.
  5. For any non-null reference value x, x.equals(null) should return false.
  6. Objects of the same class are used for comparison

Our code will fail for 5 and 6 points, here we are modifying program so it doesn’t break for 5 and 6 points

class Student {
	private int marks;

	public int getMarks() {
		return marks;
	}

	public void setMarks(int var) {
		this.marks = var;
	}

	// Overriding equals method
	@Override
	public boolean equals(Object obj) {

		// to check for null
		if (obj == null) {
			return false;
		}

		// to check for same class
		if (getClass() != obj.getClass())
			return false;
		
		// finally compare object if all goes well
		Student st = (Student) obj;
		if (st.getMarks() != this.marks)
			return false;
		return true;
	}
}

public class TestClass {

	public static void main(String[] args) {

		Student john = new Student();
		john.setMarks(10);

		Student kevin = new Student();
		kevin.setMarks(10);

		if (john.equals(kevin))
			System.out.println("Marks are same for both students");
		else
			System.out.println("Marks are not same for both students");
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *