WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Core Java · Intermediate · question 36 of 100

What is the difference between equals() and hashCode() methods in Java?

📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

In Java, equals() and hashCode() are two methods that are used to compare objects. While both methods are related to object comparison, they serve different purposes and must be implemented appropriately to ensure correct behavior.

The equals() method is used to determine whether two objects are equal in terms of their values. By default, the equals() method compares object references to determine equality, but this behavior can be overridden to provide custom comparison logic. When overriding the equals() method, it’s important to ensure that the comparison is consistent with the hashCode() method, as objects that are equal must have the same hash code.

Here’s an example of how to override the equals() method:

public class MyClass {
    private int id;
    private String name;
    
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof MyClass)) {
            return false;
        }
        MyClass other = (MyClass) obj;
        return id == other.id \&\& Objects.equals(name, other.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}

In this example, we override the equals() method to compare MyClass objects based on their id and name fields. We also override the hashCode() method to ensure that objects with equal fields have the same hash code.

The hashCode() method, on the other hand, is used to compute a hash code value for an object. The hash code is used by data structures such as hash tables to quickly locate objects based on their keys. By default, the hashCode() method returns a hash code based on the object’s memory address, but this behavior can be overridden to provide custom hash code computation.

Here’s an example of how to override the hashCode() method:

public class MyClass {
    private int id;
    private String name;
    
    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}

In this example, we override the hashCode() method to compute a hash code based on the id and name fields of the MyClass object.

In summary, the equals() method is used to determine whether two objects are equal in terms of their values, while the hashCode() method is used to compute a hash code value for an object. Both methods must be implemented appropriately to ensure correct behavior in data structures such as hash tables.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Core Java interview — then scores it.
📞 Practice Core Java — free 15 min
📕 Buy this interview preparation book: 100 Core Java questions & answers — PDF + EPUB for $5

All 100 Core Java questions · All topics