Full Stack Developer (Java) Question:
Download Questions PDF

Please explain what is the contract between equals and hashCode of an object?

Answer:

The only obligation is that for any objects o1 and o2 then if o1.equals(o2) is true then o1.hashCode() == o2.hashCode() is true.

Note that this relationship goes only one way: for any o1, o2 of some class C, where none of o1 and o2 are null, then it can happen that o1.hashCode() == o2.hashCode() is true BUT o1.equals(o2) is false.

Download Full Stack Developer (Java) Interview Questions And Answers PDF

Previous QuestionNext Question
Explain me what is the difference between String, StringBuffer andStringBuilder?Explain me what’s the difference between a ClassNotFoundException and NoClassDefFoundError?