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

Discussion

What a negative return from compareTo means - which of the following is true?

  • A.The two objects cannot be compared, so the caller should fall back on equals.
  • B.The two objects differ by that many positions in the sorted order.
  • C.This object sorts before the argument; only the sign matters, not the magnitude.
  • D.This object sorts after the argument; only the sign matters, not the magnitude.

Answer: C

compareTo returns a negative number, zero or a positive number, and callers are expected to test only the sign. Returning a difference such as this.age - other.age works most of the time but overflows for extreme values, which is why Integer.compare is the safer idiom.

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

Post your comment

Please log in or register to post a comment.