Overriding equals method in Java Last Updated : 29 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice Consider the following Java program: Java class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } } // Driver class to test the Complex class public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); Complex c2 = new Complex(10, 15); if (c1 == c2) { System.out.println("Equal "); } else { System.out.println("Not Equal "); } } } Output: Not Equal The reason for printing "Not Equal" is simple: when we compare c1 and c2, it is checked whether both c1 and c2 refer to same object or not (object variables are always references in Java). c1 and c2 refer to two different objects, hence the value (c1 == c2) is false. If we create another reference say c3 like following, then (c1 == c3) will give true. Java Complex c3 = c1; // (c3 == c1) will be true So, how do we check for equality of values inside the objects? All classes in Java inherit from the Object class, directly or indirectly (See point 1 of this). The Object class has some basic methods like clone(), toString(), equals(),.. etc. We can override the equals method in our class to check whether two objects have same data or not. Java class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } // Overriding equals() to compare two Complex objects @Override public boolean equals(Object o) { // If the object is compared with itself then return true if (o == this) { return true; } /* Check if o is an instance of Complex or not "null instanceof [type]" also returns false */ if (!(o instanceof Complex)) { return false; } // typecast o to Complex so that we can compare data members Complex c = (Complex) o; // Compare the data members and return accordingly return Double.compare(re, c.re) == 0 && Double.compare(im, c.im) == 0; } } // Driver class to test the Complex class public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); Complex c2 = new Complex(10, 15); if (c1.equals(c2)) { System.out.println("Equal "); } else { System.out.println("Not Equal "); } } } Output: Equal As a side note, when we override equals(), it is recommended to also override the hashCode() method. If we don't do so, equal objects may get different hash-values; and hash based collections, including HashMap, HashSet, and Hashtable do not work properly (see this for more details). We will be covering more about hashCode() in a separate post. References: Effective Java Second Edition Comment More infoAdvertise with us Next Article Period equals() method in Java with Examples K kartik Follow Improve Article Tags : Java java-overriding Practice Tags : Java Similar Reads Set equals() Method in Java In Java, the equals() method of the Set class is used to compare two sets for equality. It checks if two sets contain the same elements regardless of their order.Example 1: This example demonstrates how to compare two HashSet collections for equality of type string using the equals() method.Java// J 2 min read YearMonth equals() method in Java The equals() method of YearMonth class in Java is used to compare two YearMonth objects. It compares this YearMonth object to the YearMonth object passed to it as parameter and checks if two YearMonth instances are equal or not. Syntax: public boolean equals(Object otherYearMonth) Parameter: This me 2 min read Method Class | equals() Method in Java The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if the Method object is the same as the passed object. Two Methods are the same if they were declared by 3 min read Period equals() method in Java with Examples The equals() method of Period class in Java is used to check if two given periods are equal or not. The comparison is based on the type Period and each of the three years, months and date. To be equal, all of the three year, month and date must be individually equal. Syntax: public boolean equals(Pe 2 min read ZoneId equals() method in Java with Examples equals() method of the ZoneId class used to compare this ZoneId to the ZoneId object passed as parameter. The value to be returned by this method is determined as follows: if both ZoneId are equal, then true is returned if both ZoneId are not equal, then false is returned. Syntax: public boolean equ 2 min read UUID equals() Method in Java with Examples The equals() method of UUID class in Java is used to check for the equality of one UUID with another. The method returns True if both the UUID's are exactly identical bit by bit. Syntax: public boolean equals(Object uuidObj) Parameters: The method takes one parameter uuidObj to which the UUID_1 is t 2 min read Like