DEV Community

Cover image for Understanding the Difference Between `==` and `equals()` in Java
Nikhil Soman Sahu
Nikhil Soman Sahu

Posted on

Understanding the Difference Between `==` and `equals()` in Java

In Java, the == operator and the equals() method are both used for comparing objects, but they have different behaviors and use cases. Understanding the difference between these two is crucial for writing correct and efficient code, especially when dealing with object comparisons.

The == Operator

The == operator is used to compare two values or object references for equality. When used with primitive data types like int, double, boolean, etc., it compares the actual values stored in those variables. However, when used with object references, the == operator compares the memory addresses of the objects, not their actual content or state.

Here's an example:

String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");

System.out.println(str1 == str2); // Output: true
System.out.println(str1 == str3); // Output: false
Enter fullscreen mode Exit fullscreen mode

In the above example, str1 and str2 reference the same String object in the String pool, so str1 == str2 evaluates to true. However, str3 is a new String object created in the heap memory, so str1 == str3 evaluates to false even though the content of the strings is the same.

The equals() Method

The equals() method is defined in the Object class and can be overridden by subclasses to provide their own implementation of equality comparison. By default, the equals() method in the Object class performs the same reference comparison as the == operator. However, classes like String, Integer, and other wrapper classes override the equals() method to compare the actual content or state of the objects.

Here's an example using the String class:

String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");

System.out.println(str1.equals(str2)); // Output: true
System.out.println(str1.equals(str3)); // Output: true
Enter fullscreen mode Exit fullscreen mode

In this case, both str1.equals(str2) and str1.equals(str3) evaluate to true because the equals() method in the String class compares the character sequences of the strings, not their references.

When to Use == and equals()

As a general rule, you should use the == operator for comparing primitive data types and the equals() method for comparing objects, unless you specifically want to compare object references instead of their content.

It's important to note that when you create your own classes, the equals() method from the Object class will perform a reference comparison, just like the == operator. If you want to implement a custom equality comparison for your class, you should override the equals() method and provide your own implementation.

Here's an example of overriding the equals() method in a custom class:

class Person {
    private String name;
    private int age;

    // Constructor, getters, and setters

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Person other = (Person) obj;
        return name.equals(other.name) && age == other.age;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the equals() method is overridden to compare the name and age fields of Person objects. Two Person objects are considered equal if their name strings are equal (using the String class's equals() method) and their age values are the same.

Conclusion

The == operator and the equals() method serve different purposes in Java. The == operator compares the memory addresses (references) of objects, while the equals() method compares the actual content or state of objects (when overridden appropriately). When working with objects, it's generally recommended to use the equals() method unless you specifically want to compare object references. Understanding this distinction is crucial for writing correct and efficient code, especially when dealing with object comparisons in Java.

Top comments (3)

Collapse
 
freya_9b0b8bdffed2180337e profile image
Freya

In Java, == and equals() are used for comparison, but they serve different purposes:

== Operator:

Compares references (memory addresses) of two objects.
Returns true if both references point to the same object in memory.
Works for both primitives and object references.
Example:

java
Copy code
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2); // Output: false (different references)
equals() Method:

Compares the content of two objects.
Returns true if the objects are logically equivalent (i.e., they have the same values).
Defined in the Object class and can be overridden in user-defined classes to provide custom equality logic.
Example:

java
Copy code
System.out.println(str1.equals(str2)); // Output: true (same content)
Key Takeaways:
Use == to check if two references point to the same object.
Use equals() to check if two objects are logically equal based on their content.
This distinction is crucial for proper object comparison in Java, especially when working with strings and custom objects.

Collapse
 
mickhence profile image
Mark henry

Understanding the Difference Between == and equals() in Java

Collapse
 
mia_luna_2674ffd0e985af4b profile image
Mia Luna

its really helpfull me