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
What is finally and finalize in java?
hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea
What is mvc in java?
What is a variable declaration?
What is skeleton and stub?
How does linkedhashmap work in java?
What are the advantages of exception handling?
Can vector have duplicates in java?
What is the significance of listiterator?
What is a pointer and does java support pointers?
what is meant by Byte code concept in Java?
What is deserialization?
When do we use hashset over treeset?
What is the use of default method in interface in java?
Can extern variables be initialized?