Why do we need to override equals() and hascode() method of object class?
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 |
Can a class have multiple superclasses?
What are the difference between string, string builder, and string buffer in java?
How multi processing is achieved in JAVA?
What is map java?
What is a protected method?
how exactly garbage collection take place?
What is a parameter in matrices?
what is the difference between abstract class and Interface?where we can use it in realtime projects?
23 Answers Agile Software, Cognizant, IBM, Innodata, Logica CMG, Mitosis, Nagarro, Value Chain,
what are three ways in which a thread can enter the waiting state? Or what are different ways in which a thread can enter the waiting state? : Java thread
What is object-oriented programming?
What is the default value of local and global variables?
What are the methods used to implement for the key object in the hash map?