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

Discussion

Consider what Objects.equals(a, b) does that a.equals(b) does not. Which statement is correct?

  • A.It throws NullPointerException when either argument is null, making the bug obvious.
  • B.It handles a null first argument, returning true only when both arguments are null.
  • C.It compares the objects field by field using reflection.
  • D.It compares hash codes first, and only calls equals when they match.

Answer: B

The helper exists to remove the null check at the call site: two nulls are equal, a null and a non-null are not, and otherwise it delegates to the first argument's equals. Calling a.equals(b) directly is safe only when a is known not to be null.

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

Post your comment

Please log in or register to post a comment.