== vs equals in Java: Key Differences and When to Use Each
== 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() |
|---|---|---|
| Purpose | Checks if two references point to the same object | Checks if two objects are logically equal |
| Type of Comparison | Reference comparison | Content (value) comparison |
| Applicable To | Primitives and object references | Objects only |
| Default Behavior | Compares memory addresses | Compares memory addresses unless overridden |
| Can Be Overridden | No | Yes, by overriding in class |
| Null Safety | Throws error if comparing with null on primitives | Can 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
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 } }
equals() Method Example
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 } }
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.== for primitives and reference identity checks.equals() for comparing object values and override it in custom classes.equals() behaves like == unless overridden.