What is the difference between == and equals() in Java?
In Java, both `==` and `equals()` are used to compare values, but they operate differently, especially when dealing with objects. Understanding their distinctions is crucial for correct object comparison and avoiding common programming pitfalls.
The `==` Operator
The == operator is primarily used for comparing two values. Its behavior depends on whether the operands are primitive types or object references.
For primitive data types (like int, char, boolean, float, double), == compares their actual values. If the values are identical, it returns true; otherwise, false.
int a = 10;
int b = 10;
int c = 20;
System.out.println(a == b); // true
System.out.println(a == c); // false
For objects, == compares the memory addresses (references) of the objects. It returns true only if both references point to the exact same object in memory. It does not compare the content or state of the objects.
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = s1;
System.out.println(s1 == s2); // false (different objects in memory)
System.out.println(s1 == s3); // true (s1 and s3 refer to the same object)
The `equals()` Method
The equals() method is defined in the Object class, which is the superclass of all Java classes. By default, the equals() method in the Object class behaves exactly like the == operator for objects: it compares object references.
However, most standard Java classes (like String, Integer, Date, etc.) override the equals() method to provide a meaningful comparison of the objects' content or state, rather than just their memory addresses.
class MyObject {
int value;
MyObject(int value) { this.value = value; }
}
MyObject obj1 = new MyObject(10);
MyObject obj2 = new MyObject(10);
System.out.println(obj1.equals(obj2)); // false (default equals() compares references)
When equals() is overridden, it typically compares the values of the instance variables to determine if two objects are logically equal.
String str1 = new String("java");
String str2 = new String("java");
System.out.println(str1.equals(str2)); // true (String class overrides equals() to compare content)
Key Differences Summarized
| Feature | `==` Operator | `equals()` Method |
|---|---|---|
| Purpose | Compares values for primitives, references for objects. | Compares content/state for overridden classes; references for default `Object` implementation. |
| Applicability | Works with both primitive types and object references. | Works only with object references (method call). |
| Default Behavior (Objects) | Compares memory addresses. | Compares memory addresses (from `Object` class). |
| Overriding | Cannot be overridden. | Can and often should be overridden for custom classes. |
| Type | Operator | Method of `Object` class. |
When to Use Which?
- Use
==when comparing two primitive values (e.g.,int,boolean). - Use
==when you specifically want to check if two object references point to the exact same object in memory. - Use
equals()when you want to compare the content or logical equivalence of two objects. This is the most common use case for objects. - Always be aware of whether the
equals()method has been overridden in the class you are working with. If not overridden, it behaves like==.