Machine-translated by Google. Questions are authored in English.
Top

Discussion

What the hashCode contract requires of two equal objects - which of the following is true?

  • A.Two objects that are equal must return hash codes that differ by a constant.
  • B.Two objects that are equal must return the hash code of their declaring class.
  • C.Two objects that are equal must return the same hash code.
  • D.Two objects that are equal may return different hash codes if neither is used as a map key.

Answer: C

This is the one direction the contract insists on: if equals says two objects are the same, hashCode must agree by producing the same integer. A hash-based collection finds a bucket from the hash and only then compares with equals, so violating it makes an entry unreachable.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.