Practice mode Java

If two Java objects are equal according to equals, what must be true of their hashCode values?

Correct answer C. They must be the same

Explanation

The Object contract requires equal objects to return equal hash codes, otherwise hash-based collections look in the wrong bucket and fail to find them. The reverse is not required, as unequal objects may share a hash code. This is why a class that overrides equals should always override hashCode too.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.