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

Name the method of a Container that can be used to cause a container to be laid out and redisplayed?

1 Answers  


How to perform Singleton of the java class object on multi JVM?

2 Answers  


Is there any case when finally will not be executed?

0 Answers  


What is a cup of java?

0 Answers  


if num=687; U have to get num=6+8+7;

4 Answers   CMC,


How many unicode characters are there?

0 Answers  


What is int lol?

0 Answers  


Give example to differentiate between call by value and call by reference.

0 Answers   TCS,


Why string is not a wrapper class?

0 Answers  


What is the relationship between class and object?

0 Answers  


Difference difference paint() and paintcomponent()?

0 Answers  


Is Cegonsoft Pvt.Ltd. a good Institute?

4 Answers  


Categories