What is the correct way to override the equals method in Java?
I have a class People
with two fields: name
(a String
) and age
(an Integer
). I want to properly override the equals method in Java to compare two People
objects.
Here’s the code I tried:
public boolean equals(People other) {
boolean result;
if ((other == null) || (getClass() != other.getClass())) {
result = false;
} else {
People otherPeople = (People) other;
result = name.equals(other.name) && age.equals(other.age); // Error here
}
return result;
}
However, I get an error when calling age.equals(other.age)
, since equals()
is typically used for objects like String
, but age
is an Integer
.
How should I properly override the equals method in Java to handle this comparison correctly?
I’ve worked with Java for a while, and when dealing with custom objects, overriding equals()
properly is key. I noticed you’re running into issues because you’re using equals()
to compare age
, which is an Integer
. Since Integer
supports equals()
, your approach is mostly right, but the method signature needs to match Object
, not People
."*
Here’s the corrected version:
@Override
public boolean equals(Object obj) { // Must accept Object, not People
if (this == obj) return true; // Same reference check
if (obj == null || getClass() != obj.getClass()) return false;
People other = (People) obj;
return name.equals(other.name) && age.equals(other.age);
// Fix: age is an Integer, so equals() works properly
}
Why this works?
- Uses
Object
as the parameter (necessary for correct overriding).
- Compares
name
and age
correctly using equals()
.
- Includes a reference check (
this == obj
) for performance optimization.
That’s a solid implementation, @akanshasrivastava.1121! One way to make it even more readable and null-safe is by using Objects.equals()
from Java’s java.util
package. It automatically handles null checks, making the code cleaner and safer.
Here’s the improved version:
import java.util.Objects;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
People other = (People) obj;
return Objects.equals(name, other.name) && Objects.equals(age, other.age);
}
Why use this?
- No need for explicit null checks—
Objects.equals()
does it for you.
- More readable, especially when comparing multiple fields.
This approach enhances your java override equals
implementation with better readability and safety.
Great points, @netra.agarwal To follow best practices, it’s essential to override hashCode()
alongside equals()
. This ensures consistency when using collections like HashSet
or HashMap
.
Here’s the complete implementation:
import java.util.Objects;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
People other = (People) obj;
return Objects.equals(name, other.name) && Objects.equals(age, other.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
Why include hashCode()
?
- Ensures objects behave correctly in hash-based collections (
HashSet
, HashMap
).
- Maintains the contract between
equals()
and hashCode()
to prevent unexpected behavior.
By adding hashCode()
, your java override equals
implementation is now complete and collection-safe.