Popular Posts

July 15, 2024

Difference between == and .equals() method in Java

 

In Java, == and the .equals() method are used to compare objects, but they serve different purposes and work in different ways. Here's a detailed explanation of their differences:

== Operator

Purpose:

  • The == operator is used to compare primitive data types and object references.

Usage:

  • Primitive Data Types: For primitive data types (e.g., int, float, char), == compares the actual values.
  • Object References: For object references, == checks if the two references point to the same memory location (i.e., it checks for reference equality).

Example:

// Comparing primitive data types

int a = 10;

int b = 10;

System.out.println(a == b); // Output: true


// Comparing object references

String str1 = new String("hello");

String str2 = new String("hello");

System.out.println(str1 == str2); // Output: false (different objects in memory)


String str3 = str1;

System.out.println(str1 == str3); // Output: true (same object in memory)


.equals() Method

Purpose:

  • The .equals() method is used to compare the contents (or state) of two objects for logical equality.

Usage:

  • Default Behavior: The default implementation of .equals() in the Object class checks for reference equality (like ==).
  • Overridden Behavior: Many classes, such as String, Integer, and user-defined classes, override .equals() to compare the values contained within the objects rather than their memory addresses.

Example:

// Comparing object contents

String str1 = new String("hello");

String str2 = new String("hello");

System.out.println(str1.equals(str2)); // Output: true (same content)


Integer num1 = new Integer(100);

Integer num2 = new Integer(100);

System.out.println(num1.equals(num2)); // Output: true (same value)


// Custom class example

class Person {

    String name;

    

    Person(String name) {

        this.name = name;

    }

    

    @Override

    public boolean equals(Object obj) {

        if (this == obj) {

            return true;

        }

        if (obj == null || getClass() != obj.getClass()) {

            return false;

        }

        Person person = (Person) obj;

        return name.equals(person.name);

    }

}


Person p1 = new Person("John");

Person p2 = new Person("John");

System.out.println(p1.equals(p2)); // Output: true (same name content)


Key Differences

  1. Comparison Basis:

    • ==: Compares memory addresses (reference equality) for objects and actual values for primitive types.
    • .equals(): Compares logical equality (the contents of objects) when properly overridden.
  2. Default Behavior:

    • ==: Always checks reference equality for objects.
    • .equals(): By default, checks reference equality, but can be overridden to check logical equality.
  3. Common Use Cases:

    • ==: Best suited for comparing primitive data types and checking if two references point to the same object.
    • .equals(): Best suited for comparing the contents of objects, such as strings, numbers, or custom objects.
  4. Overriding:

    • ==: Cannot be overridden as it is an operator.
    • .equals(): Can and often should be overridden in user-defined classes to provide meaningful equality checks.

Practical Example

public class ComparisonExample {
    public static void main(String[] args) {
        String s1 = new String("example");
        String s2 = new String("example");

        // Comparing references
        System.out.println(s1 == s2); // Output: false

        // Comparing contents
        System.out.println(s1.equals(s2)); // Output: true
    }
}

In this example, s1 == s2 is false because s1 and s2 are different objects in memory, whereas s1.equals(s2) is true because the content of the strings is the same.

Understanding the difference between == and .equals() is crucial for correctly comparing objects and values in Java.


No comments:
Write comments