Full Stack Developer (Java) Question:

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

Full Stack Developer (Java) Interview Question
Full Stack Developer (Java) Interview Question

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.


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