The equals Method

In Java, there are two types of object comparison operators:

  • Equality Operator (==) - The equality operator (==) compares the references of two objects to determine if they are the same object in memory. It checks whether two object references point to the same memory location. It returns true if the references are equal, and false otherwise.

Example:

package EqualsMethod; public class EqualOperatorDemo { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1 == str2); // true (Both references point to the same "Hello" object in the string pool) System.out.println(str1 == str3); // false (str1 points to a different object than str3) } }

Output:

 

In the example above, str1 and str2 both reference the same object in the string pool, so str1 == str2 returns true. However, str1 and str3 reference different objects, even though they have the same value, so str1 == str3 returns false.

 

Equals Method (equals()) - The equals() method is used to compare the content or value of two objects, rather than their references. It is a method defined in the Object class and can be overridden by subclasses to provide a custom implementation for comparing objects.

The equals method in Java is used to compare two objects for equality. It's like comparing two pieces of a puzzle to see if they fit together perfectly.

Let's use an everyday example: Suppose you have two cups. At a glance, they might look similar, but to check if they are truly identical, you would compare their features — like color, size, shape, and design. If all these features match, then you can conclude that the two cups are equal.

In Java, objects are like these cups. The equals method allows you to define what it means for two objects to be considered "equal." By default, the equals method in Java checks if two references (think of them as addresses or pointers) point to the exact same object, which is like checking if two addresses point to the same house.

However, you can override the equals method to compare objects based on their internal states (their properties). For example, if you have an object that represents a book, you might decide that two book objects are equal if they have the same title and author, regardless of whether they are the same physical book (or object) in memory.

So, the equals method in Java is a way to define equality for objects based on your specific criteria, going beyond just checking if they are the same physical entity in memory.

Example:

package EqualsMethod; public class EqualOperatorDemoTwo { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1.equals(str2)); // true (The content of the strings is the same) System.out.println(str1.equals(str3)); // true (The content of the strings is the same) } }

Output:

 

In the example above, both str1.equals(str2) and str1.equals(str3) return true because the equals() method compares the content of the strings, which is the same in both cases.

It's important to note that for some classes, such as String, the equals() method is overridden to provide a meaningful comparison of object content. However, for custom classes, you may need to override the equals() method to define your own comparison logic based on the object's properties.

Remember to use the appropriate comparison operator (==) for comparing object references and the equals() method for comparing object content, based on your specific requirements.

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark