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

Is there any difference between synchronized methods and synchronized statements?

0 Answers  


What is the synchronized method modifier?

0 Answers  


What is OOP's Terms with explanation?

0 Answers  


Why does java does not support multiple inheritance? Explain

0 Answers  


What is meant by throwing an Exception?

4 Answers   Accenture,






What is procedure writing?

0 Answers  


Hi Friends, can u give code to convert ArrayList to HashMap and HashMap to ArrayList.

1 Answers  


Which list is sorted in java?

0 Answers  


What does java stand for?

0 Answers  


question on Thread synchronization

3 Answers   Huawei,


What are the OOAD concepts in java explain with examples?

4 Answers   Aricent, CTS,


What is object-oriented paradigm?

0 Answers  


Categories