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
How do you replace a string in java?
Is a method a function?
How do you include a string in java?
Why is stringbuffer thread safe?
How can we make sure main() is the last thread to finish in java program?
What is the access scope of a protected method?
Explain some best practices you would apply while using collection in java?
What is an class?
Are generics important java?
take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).
Differentiate storage classes on the basis of their scope?
What does g mean in regex?
How to create a base64 decoder in java8?
what is the significance of listiterator in java?
What is array size in java?