0
0
JavaComparisonBeginner · 4 min read

== vs equals in Java: Key Differences and When to Use Each

In Java, == checks if two references point to the exact same object in memory, while equals() checks if two objects are logically equal based on their content. Use == for reference comparison and equals() for value comparison.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of == and equals() in Java.

Aspect==equals()
PurposeChecks if two references point to the same objectChecks if two objects are logically equal
Type of ComparisonReference comparisonContent (value) comparison
Applicable ToPrimitives and object referencesObjects only
Default BehaviorCompares memory addressesCompares memory addresses unless overridden
Can Be OverriddenNoYes, by overriding in class
Null SafetyThrows error if comparing with null on primitivesCan handle null safely if coded properly
⚖️

Key Differences

The == operator compares whether two variables refer to the exact same object in memory. This means it checks if both references point to the same location. For primitive types like int or boolean, == compares their actual values.

On the other hand, equals() is a method defined in the Object class that can be overridden by classes to define what it means for two objects to be "equal" in terms of their content. For example, two different String objects with the same characters are considered equal by equals() but not by ==.

By default, if a class does not override equals(), it behaves like == and compares references. But many classes like String, Integer, and custom classes override equals() to compare meaningful data inside the objects.

💻

== Operator Example

java
public class EqualsVsDoubleEquals {
    public static void main(String[] args) {
        String a = new String("hello");
        String b = new String("hello");
        String c = a;

        System.out.println(a == b); // false, different objects
        System.out.println(a == c); // true, same object

        int x = 5;
        int y = 5;
        System.out.println(x == y); // true, primitive values compared
    }
}
Output
false true true
💻

equals() Method Example

java
public class EqualsVsDoubleEquals {
    public static void main(String[] args) {
        String a = new String("hello");
        String b = new String("hello");

        System.out.println(a.equals(b)); // true, content is same

        Integer x = 100;
        Integer y = 100;
        System.out.println(x.equals(y)); // true, values are equal
    }
}
Output
true true
🎯

When to Use Which

Choose == when you want to check if two references point to the exact same object, such as when comparing enums or checking if an object is null.

Choose equals() when you want to compare the actual content or logical equality of two objects, like comparing two strings or custom objects.

Remember, for custom classes, override equals() to define meaningful equality, and always use equals() for value comparison to avoid bugs.

Key Takeaways

== checks if two references point to the same object in memory.
equals() checks if two objects are logically equal based on their content.
Use == for primitives and reference identity checks.
Use equals() for comparing object values and override it in custom classes.
Default equals() behaves like == unless overridden.