What do the return values of Comparable.compareTo mean in Java?

Viewed 175941

What is the difference between returning 0, returning 1 and returning -1 in compareTo() in Java?

7 Answers

Answer in short: (search your situation)

  • 1.compareTo(0) (return: 1)
  • 1.compareTo(1) (return: 0)
  • 0.comapreTo(1) (return: -1)

System.out.println(A.compareTo(B)>0?"Yes":"No")

if the value of A>B it will return "Yes" or "No".

Related