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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Which is illegal identifier in java?

793


Does google use java?

742


Is linked list a linear or non-linear data structure?

741


why are there separate wait and sleep methods? : Java thread

780


What is the difference between an argument and a parameter?

755


Give an example of use of pointers in java class.

760


Can static methods be inherited?

777


how to prepare for IT Officers Interview in Banks

1714


You're given a Boolean 2D matrix, can you find the number of islands?

845


What are the legal operands of the instanceof operator?

785


What means public static?

781


What are the elements of java?

782


How many threads can java run?

814


What is yield () in java?

665


Explain what access modifiers can be used for variables?

805