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 |
What are some examples of variable costs?
Name the packages in JDK?
Is void a wrapper class?
What is method overloading with type promotion?
When is an object subject to garbage collection?
Can we use String with switch case?
What is multi level inheritance in java?
What is the use of using enum to declare a constant?
Why does java not allow multiple public classes in a java file ?
When do you get classcastexception?
What is the preferred size of a component in java programming?
What is an object’s lock and which object’s have locks?