Implementation of hashCode()
and equals()
in Object
Class
The Object
class in Java provides default implementations of hashCode()
and equals()
, which are inherited by all Java classes unless explicitly overridden.
1️⃣ equals()
Method in Object
Default Implementation
public boolean equals(Object obj) {
return (this == obj);
}
How It Works
- The default
equals()
method inObject
compares memory addresses (references) using==
. - This means that two objects are considered equal only if they are the exact same instance in memory.
Example: Default equals()
Behavior
class Person {
String name;
Person(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
System.out.println(p1.equals(p2)); // ❌ false (Different objects)
System.out.println(p1 == p2); // false (Same as equals() in Object)
}
}
Since equals()
in Object
behaves like ==
, it does not check for logical equality (like comparing field values).
2️⃣ hashCode()
Method in Object
Default Implementation
public native int hashCode();
- This method is native, meaning it is implemented in C++ inside the JVM, not in Java.
- The default
hashCode()
typically returns a unique integer identifier for each object, based on its memory address.
Example: Default hashCode()
Behavior
class Person {
String name;
Person(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
System.out.println(p1.hashCode()); // ❌ Different hash codes (based on memory location)
System.out.println(p2.hashCode()); // ❌ Different hash codes
}
}
Since hashCode()
in Object
is based on memory location, different objects will have different hash codes, even if their contents are the same.
🔹 Why Override equals()
and hashCode()
?
If you do not override these methods:
equals()
checks reference equality (==
) instead of checking meaningful content.hashCode()
returns different values for objects that should be considered equal.
This breaks hash-based collections like HashSet
and HashMap
, which rely on hashCode()
and equals()
for correctness.
🔄 Correct Implementation: Overriding equals()
and hashCode()
Example: Properly Overriding Both Methods
import java.util.Objects;
class Person {
private String name;
private 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 && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice", 30);
Person p2 = new Person("Alice", 30);
System.out.println(p1.equals(p2)); // ✅ true (Now checks values)
System.out.println(p1.hashCode()); // ✅ Same hashCode
System.out.println(p2.hashCode()); // ✅ Same hashCode
}
}
📌 Summary Table
Method | Default Behavior (Object Class) | Why Override? |
---|---|---|
equals() | Uses == , compares memory addresses (reference equality). | To check logical equality (compare field values). |
hashCode() | Returns a unique integer based on memory location. | To ensure equal objects have the same hash code, making hash-based collections work correctly. |
✅ Best Practices
✔ Always override hashCode()
when overriding equals()
to maintain consistency.
✔ Use Objects.hash()
for a simple and effective hashCode()
implementation.
✔ Use Objects.equals()
to handle null
values safely inside equals()
.
By following these principles, your Java objects will work correctly in hash-based collections and follow best practices for equality checks! 🚀