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 theObject
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
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.
Default Behavior:
==
: Always checks reference equality for objects..equals()
: By default, checks reference equality, but can be overridden to check logical equality.
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.
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
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