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

Discussion

Which one of the following statements about what happens when equals is overridden but hashCode is not is true?

  • A.Two equal objects may land in different buckets.
  • B.The compiler rejects the class.
  • C.hashCode is derived from the new equals automatically.
  • D.Every lookup falls back to reference comparison.

Answer: A

Nothing stops the code compiling, and the damage only shows in a hash-based collection: two objects that equals calls the same still carry the inherited identity hash codes, so they hash to different buckets and are never compared. The javadoc notes it is generally necessary to override hashCode whenever equals is overridden.

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

Post your comment

Please log in or register to post a comment.