In Java, comparing two objects can be done in several ways depending on whether you want to check object reference equality or content equality. Here’s a detailed guide:
1. Compare Object References (Using ==
)
The ==
operator checks if two object references point to the same memory location (i.e., if they are the exact same object).
Example:
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
System.out.println("Objects are the same.");
} else {
System.out.println("Objects are different."); // This will be printed
}
- Limitation:
==
does not compare the content of the objects but only their memory addresses.
2. Compare Object Content (Using .equals()
Method)
The .equals()
method is used to compare the content or value of two objects. By default, it checks reference equality unless overridden in a class (e.g., String
, Integer
, etc.).
Example:
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1.equals(str2)) {
System.out.println("Objects are equal."); // This will be printed
} else {
System.out.println("Objects are not equal.");
}
3. Compare Objects Using the Comparable
Interface
The Comparable
interface provides the compareTo()
method for comparing objects. It is typically used to define the natural order of objects.
Example:
public class Person implements Comparable<Person> {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person other) {
return this.age - other.age; // Compare based on age
}
}
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
if (p1.compareTo(p2) > 0) {
System.out.println(p1.name + " is older than " + p2.name);
} else {
System.out.println(p1.name + " is younger than or the same age as " + p2.name); // Printed
}
4. Compare Objects Using the Comparator
Interface
The Comparator
interface allows custom comparison logic by implementing the compare()
method.
Example:
import java.util.Comparator;
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Comparator for comparing by name
class NameComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
}
// Comparator for comparing by age
class AgeComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.age - p2.age;
}
}
// Example usage
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
NameComparator nameComparator = new NameComparator();
AgeComparator ageComparator = new AgeComparator();
System.out.println("Compare by Name: " + nameComparator.compare(p1, p2)); // Negative value
System.out.println("Compare by Age: " + ageComparator.compare(p1, p2)); // -5
5. Overriding .equals()
and .hashCode()
To compare custom objects for content equality, override the .equals()
and .hashCode()
methods.
Example:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
// Example usage
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Alice", 25);
if (p1.equals(p2)) {
System.out.println("Objects are equal."); // Printed
} else {
System.out.println("Objects are not equal.");
}
Summary of Methods:
Method | Purpose | Comparison Type |
---|---|---|
== |
Compares memory references | Object reference equality |
.equals() |
Compares content/value (can be overridden) | Object content equality |
compareTo() |
Compares natural order (uses Comparable ) |
Sorting and ordering |
Comparator |
Allows custom comparison logic | Sorting and ordering |