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

Discussion

Consider the five properties the equals contract requires. Which statement is correct?

  • A.equals must be reflexive and consistent, but symmetry is required only of final classes.
  • B.equals must be reflexive, symmetric, transitive, consistent, and false for a null argument.
  • C.equals must be reflexive, symmetric and transitive, but may return true for a null argument.
  • D.equals must be symmetric and transitive, but need not be reflexive for a mutable object.

Answer: B

The javadoc lists exactly five requirements: x.equals(x) is true, x.equals(y) agrees with y.equals(x), equality carries through a chain, repeated calls give the same answer while the objects are unchanged, and x.equals(null) is false. Breaking any of them makes an object misbehave in a collection.

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

Post your comment

Please log in or register to post a comment.