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 are different types of constants?

0 Answers  


Can you inherit from an abstract class java?

0 Answers  


What is the char data type?

0 Answers  


What is immutable class in java?

0 Answers  


If there are two notepad opened and working on it . is this a process or thread? computer based Live Example to differentiate between Thread , Process , and Multi tasking ?

3 Answers   TCS, Wipro,






Hi ,i convert contrller as jSp And presentation as servlet ...will it do? if so what are advantage and idsadvantages

2 Answers   IBM,


What is object cloning in Java?

0 Answers   SwanSoft Technologies,


Can private method static?

0 Answers  


What is the same as procedures?

0 Answers  


What is a arraylist in java?

0 Answers  


What does sprintf mean?

0 Answers  


Explain about version control?

0 Answers  


Categories