Why do we need to override equals() and hascode() method of object class?



Why do we need to override equals() and hascode() method of object class?..

Answer / 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

More Core Java Interview Questions

Difference between a Scrollbar and a ScrollPane?

1 Answers  


class A { public void disp(int a,int b) { System.out.println("hai"); } } class B { public void disp(int a,int b,int c) { System.out.println("hai"); } } above program is overloading or overriding?

9 Answers   Infosys, Wipro,


How do you compare values in java?

0 Answers  


heavy components means what?

4 Answers  


What is a function easy definition?

0 Answers  






waht happens when a return type ,even void is specified for a constructor?

11 Answers  


How do I write a self declaration?

0 Answers  


I have 2 objects inside one object(vector). how can i serialize one of them. I dont want to serialize the second one

3 Answers   ABC, L&T,


Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want to count the letters how many times it occures.i.e from the above string the out put should be like this U-1,N-2,D-2,E-1,R-1,S-1,T-1,A-1. how can i achieve this Thnaks in advance for your response ..

7 Answers  


What is inheritance?

5 Answers   Accenture, Hexaware, Syntel,


What are the types of methodology?

0 Answers  


Difference between stack and queue?

0 Answers   Flextronics,


Categories