Chet Chet - 4 months ago 23
Java Question

Java - compareTo and operators

If I have a class

Person
that implements
Comparable
(compares
personA.height
to
personB.height
, for example), is it possible to use

personA < personB


as a substitute for

personA.compareTo(personB) == -1?


Are there any issues in doing this or do I need to overload operators?

Answer

There is no operator overloading in Java. You probably come from a C++ background.

You have to use the compareTo method.

Comments