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

Who found java?

547


What is a private class in java?

504


What is an image buffer?

531


What is hotjava?

548


What is arraylist e in java?

504






What is the purpose of declaring a variable as final?

481


What is finalize() function in java?

555


How many bytes are a float?

501


What is the integer of 16?

532


What all methods are used to prevent thread execution ?

547


What is math in java?

560


What is an anonymous class in java?

538


What is unmodifiable list in java?

528


what is object slice?

1568


What is a superclass?

920