The Difference Between equals() and == in Java
The Difference Between equals() and == in Java
Description
In Java, both == and equals() are used for comparison, but their underlying logic and application scenarios are fundamentally different. Understanding their differences is essential for mastering object comparison and the memory model in Java.
Key Concepts Explanation
-
The
==Operator-
Core Function:
==is a comparison operator. -
What it Compares:
- When comparing primitive data types (e.g.,
int,double,char,boolean),==checks if their values are equal. - When comparing reference data types (i.e., objects, such as
String, custom class objects),==checks if the memory addresses (also called object references) that the two variables point to are the same. In other words, it determines whether the two variables reference the same object in heap memory.
- When comparing primitive data types (e.g.,
-
Example Analysis:
int a = 10; int b = 10; System.out.println(a == b); // Outputs true. Compares values of primitive types, 10 equals 10. String str1 = "Hello"; String str2 = "Hello"; String str3 = new String("Hello"); System.out.println(str1 == str2); // Outputs true System.out.println(str1 == str3); // Outputs false- Reason for
str1 == str2beingtrue: Due to the nature of string literals,"Hello"is placed in the string constant pool. Bothstr1andstr2point to the same"Hello"object in the constant pool, so their memory addresses are identical. - Reason for
str1 == str3beingfalse:new String("Hello")creates a brand new object in heap memory.str3points to this new object, whilestr1points to the object in the constant pool. They are two different objects, so their memory addresses are naturally different.
- Reason for
-
-
The
equals()Method- Core Function:
equals()is a method defined in theObjectclass. All Java objects inherit from theObjectclass, therefore all objects have theequals()method. - Default Behavior: In the
Objectclass, the default implementation ofequals()uses==for comparison. The source code is as follows:
This means that if a class does not override thepublic boolean equals(Object obj) { return (this == obj); }equals()method, calling itsequals()method is equivalent to using==, comparing memory addresses. - Significance of Overriding: However, we are usually concerned with whether two objects are "equal" in logical content, not whether they are the same object. Therefore, many classes in the core libraries (e.g.,
String,Integer,Date) override theequals()method to compare the content or attribute values of the objects. - Overriding
equals()in theStringClass: TheStringclass overridesequals()to compare whether the content (i.e., the sequence of characters) of the strings is identical.String str1 = "Hello"; String str3 = new String("Hello"); System.out.println(str1.equals(str3)); // Outputs true- Even though
str1andstr3are two different objects (==comparison yieldsfalse), they contain the same character sequence'H','e','l','l','o'. Thus, theequals()method compares the content and returnstrue.
- Even though
- Core Function:
-
Summary and Comparison
| Comparison Aspect | == Operator |
equals() Method |
|---|---|---|
| Nature | Comparison operator in Java | Method defined in the Object class |
| What it Compares | Primitive data types or object references | Can only be objects (cannot be used on primitive data types) |
| What it Compares (Content) | Primitive types: value Reference types: memory address |
Default comparison: memory address (same as ==)After overriding: object content (determined by the overriding logic) |
| Example Conclusion | str1 == str3 // false (not the same object) |
str1.equals(str3) // true (same content) |
Key Takeaways
- For comparing primitive data types, always use
==. - For comparing reference data types:
- If you want to determine whether two references point to the same object in memory, use
==. - If you want to determine whether two objects are logically equal in content, use
equals().
- If you want to determine whether two references point to the same object in memory, use
- In custom classes, if you need to determine equality based on object attributes, you must override the
equals()method (and typically also override thehashCode()method).