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

Why java strings are immutable in nature?

579


What does nextint () do in java?

546


What technique is carried out to find out if a particular string is empty?

557


What are kinds of processors?

558


Differentiate between run time error and syntax error.

561






What is a jit compiler?

626


What is the function of compareto in java?

581


When is the finalize() called? What is the purpose of finalization?

573


What does yield method of the thread class do?

572


Describe the Big-O Notation.

609


Explain list interface?

509


Is java type safe?

499


What is constructor chaining and how is it achieved in java?

564


Where is the singleton class used?

574


Explain about main() method in java ?

576