Why do we need to override equals() and hascode() method of object class?



Why do we need to override equals() and hascode() method of object class?..

Answer / javamasque

As per equality contract of Java if two objects are equal then they should return equal integer, means if obj1.equals(obj2) then obj1.hashCode() == obj2.hashCode();

As we override equals method we compares equality on value of each property inside 1st object with 2nd object, as it finds all properties are equal then returns true else false.

As we override hascode method we generate a unique integer by multiplying with prime number. If we multiply with prime number then there is most possibility to get unique integer. The prime number 31 is mostly used as the hascode method is override.

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Core Java Interview Questions

what is difference betwwen hashmap and hashtable ?

7 Answers   Fidelity,


What is an argument in java?

0 Answers  


Is void a data type?

0 Answers  


What is the purpose of a statement block?

1 Answers  


What is meant by overloading?

0 Answers  






What is getclass () getname () in java?

0 Answers  


What is the requirement of thread in java?

0 Answers  


Explain parallel processing in java8?

0 Answers  


Explain importance of throws keyword in java?

0 Answers  


Explain the selection sort algorithm?

0 Answers   Hexaware,


What is the difference between interface & abstract class?

0 Answers  


If try block is successfully executed, Then Is Finally block executed?

0 Answers   PUCIT,


Categories