What is difference between hash code and equals

What is difference between hash code and equals

Both hashCode and equals method is derived from Object class which is superclass of all and it belongs to java.lang package

Answer: Below are differences:

HashCodeEquals
It return hash code value of the objectIt returns true if other object is “equal to” this one otherwise returns false
Both have general contract between them which says that equal objects should have same hash codeIt follows the same general contract which says that equal objects should have same hash code
For any non-null values if “a” and “b” are equals, it means they are same object then it will return same hash code value.For any non-null values “a” and “b” this method return true if both a and b refer to the same object ( a == b is true then both have same reference)
Hash code method defined in class Object and it returns distinct integers value for distinct objectsEquals provide no information about it
Whenever hash code method invokes multiple time on the same object then it consistently returns same integer valueIt compares object reference and don’t provide its integer value
Method details: public int hashCode() – It returns hashcode of the objectMethod detail: public boolean equals(Object obj) – compare object and returns value
You cannot call hashcode method on null object it will throw nullpointerexceptionYou can compare using equals for reference with for example “a”, a.equals(null) should return false
If two objects are equals then it will return same hash codeBut if two object has same hash code they may or may not be equal

 

Please see example below:

package com.javahonk.setTest;

public class HashCodeEqualsTest {

    public static void main(String[] args) {

    Employee e1 = new Employee("Java", "1000");
    Employee e2 = new Employee("Honk", "1000");
    Employee e3 = new Employee("Bob", "3000");

    System.out.println("e1 and e2 are equal:  " + e1.equals(e2));
    System.out.println("e1 and e3 not equal:  " + e1.equals(e3));
    
    System.out.println();
    System.out.println("e1 hash code:  " + e1.hashCode());
    System.out.println("e2 hash code:  " + e2.hashCode());
    System.out.println("e3 hash code:  " + e3.hashCode());

    }

}

class Employee {

    String name;
    String salary;

    public Employee(String name, String salary) {
        super();
        this.name = name;
        this.salary = salary;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        boolean result = false;
        if (obj instanceof Employee) {
            Employee employee = (Employee) obj;
            if (this.salary == employee.salary) {
                result = true;
            }
        }
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        
        int hashcodeValue = 10;
        hashcodeValue = this.name.hashCode();
        hashcodeValue =  this.salary.hashCode();
        return hashcodeValue;
    }
    
    

}

Output:
What is difference between hash code and equals

Leave a Reply

Your email address will not be published. Required fields are marked *