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

  1. 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.
    • 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 == str2 being true: Due to the nature of string literals, "Hello" is placed in the string constant pool. Both str1 and str2 point to the same "Hello" object in the constant pool, so their memory addresses are identical.
      • Reason for str1 == str3 being false: new String("Hello") creates a brand new object in heap memory. str3 points to this new object, while str1 points to the object in the constant pool. They are two different objects, so their memory addresses are naturally different.
  2. The equals() Method

    • Core Function: equals() is a method defined in the Object class. All Java objects inherit from the Object class, therefore all objects have the equals() method.
    • Default Behavior: In the Object class, the default implementation of equals() uses == for comparison. The source code is as follows:
      public boolean equals(Object obj) {
          return (this == obj);
      }
      
      This means that if a class does not override the equals() method, calling its equals() 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 the equals() method to compare the content or attribute values of the objects.
    • Overriding equals() in the String Class: The String class overrides equals() 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 str1 and str3 are two different objects (== comparison yields false), they contain the same character sequence 'H','e','l','l','o'. Thus, the equals() method compares the content and returns true.
  3. 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().
  • In custom classes, if you need to determine equality based on object attributes, you must override the equals() method (and typically also override the hashCode() method).