Last Updated:

Comparing Objects with the Equals() Operator in Java

There are two ways to compare objects in the Java language:


You can compare objects with the equals() operator as follows:

The equals() method belongs to the Object type, which is the ancestor of each class in the Java language. This means that any class you create will inherit the basic equals() behavior from Object. This basic behavior is the same as the == . In other words, by default, these two expressions use the == operator and return false:

Look at the spendMoney() method of the Ginger class again. What happens behind the scenes when we call the contains() method of our wallet variable? The Java language uses the == operator to compare the objects in the list to the specified object. If it finds a match, the method returns true ; otherwise, false is returned. As we compare primitives, it can find a match based on integer values (remember that the == operator compares primitives by their value).

This is good for primitives, but what if we want to compare the contents of objects? The == operator will not do this. To compare the contents of objects, we must overload the equals() method of the class whose instance is the variable a . This means that you need to create a method with exactly the same signature as a method in one of your superclasses, but implement the method in a different way. If you do this, you can compare the contents of two objects, not just check if two variables refer to the same instance.

Try typing the following code into the main() method and looking at the result displayed in the console:

The first comparison returns false because Ginger1 and Ginger2 refer to different instances of Ginger. The second comparison also returns false, because the default implementation of the equals() method simply checks whether both variables refer to the same instance. But the default behavior of equals() is usually not what we want. We would like to compare the contents of two Ginger objects to see if they are the same. To do this, we can overload equals(). As you can see from the last two comparisons in the example above, the Integer class overloads the method so that the == operator returns false, but the equals() operator compares the packed int values to equality. We'll do something similar for the Ginger class in the next section.

Overload equals()

Overloading the equals() method to compare objects actually requires us to overload two methods:

We'll overload the equals() method in the following way, which is the usual style in Java:

  • If the object we're going to compare is the object itself, then they are obviously equal, so we return true .
  • Check to make sure that the object we are going to compare is an Ginger instance (if not, the two objects are obviously not the same).
  • Give the input object type to Ginger in order to use its methods.
  • Compare the parts of two Ginger objects that must be equal in order for two objects to be considered "equal" (whatever definition of equality we use).
  • If any of these parts are not equal, return false ; otherwise, return true .

Note that we can compare the age of each object using == , since these are primitive values. To compare String, we use equals() because this class overloads the equals() method to compare the contents of String objects (if we used == , we would get false every time, because two String objects will never be the same object). We do the same for the ArrayList, as it overloads equals() to verify that two lists contain the same items in the same order. This fits well for our simple example.

Whenever you overload equals(), you must also overload the hashCode(). An explanation of why this tutorial is beyond the scope of this tutorial, but for now, just know that the Java language uses the value returned from this method to place instances of your class in a collection that use a hash algorithm for arranging objects (such as HashMap). The only rule of thumb for this method (besides the fact that it must return an integer value) are the following two rules: The hashCode() method should return:

  • The same value for the same object is constant.
  • The same values for identical objects.

In general, returning hash code values for some or all of the variables in an object instance is a good enough way to compute the hash code. Another option is to convert variables to String objects, combine them, and return a hash code for the resulting String object. Another option is to multiply one or more numerical variables by some constant to further ensure uniqueness, but this is often redundant.