What is Difference between equals and hashcode method?

devquora
devquora

Posted On: Jan 08, 2021

 

The differences between The hashCode() method and The equals() method are as follows:

The hashCode() methodThe equals() method
The hashCode() method The hashCode() method returns an integer value, which is referred to as the hash code value of an object. Every Object, at the time of creation assigned with a unique 32-bit, signed int value. This value is the hash code value of that object. The hashCode() method should return the same integer value for the same object for each calling of this method unless the value stored in the object is modified. If two objects are equal(according to equals() method) then the hashCode() method should return the same integer value for both the objects. But, it is not necessary that the hashCode() method will return the distinct result for the objects that are not equal (according to equals() method).The equals() method The equals() method of Object class checks the equality of the objects and accordingly it returns true or false. The default implementation, as provided by the Object class, checks the equality of the objects on the basis if both references refer to the same object. It does not check the value or state of the objects. But we can override this method to provide our own implementation to compare the state or value of the objects. For any non-null reference variables a, b and c a.equals(a) should always return true. a.equals(b) should return true if and only if b.equals(a) returns true. If a.equals(b) returns true and b.equals(c) returns true then a.equals(c) should return true. Multiple calling of a.equals(b) should consistently return true or consistently return false If the value of the object is not modified for either object. a.equals(null) should return false.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    NIIT Technologies Java Interview Questions

    What is abstraction?

    Abstraction is basically used to separate the useless data from the essential ones. Only the important things that need to be shown to the users are only being exposed. Here in these issues, the users...

    NIIT Technologies Java Interview Questions

    What is the difference between left and right outer join?

    The difference between the left and right outer join is as follows:S. N. Left Join Outer Join1 A left outer join provides the table in the FROM clause whose all rows are returned....

    NIIT Technologies Java Interview Questions

    Explain how HashMap works?

    HashMap in Java works on hashing principles. Hashing is a process in which hash functions are used to link key and value in HashMap....