2

Is there a plug in that supports compare operations on a custom object?

I would like to use this:

class MyTemperatureObject implements Comparable<MyTemperatureObject> {
    ...
    @Override
    public int compareTo(MyTemperatureObject object) {
       return getValue().compareTo(object.getValue());
    }
}

MyTemperatureObject a;
MyTemperatureObject b;

if (a < b){
    ...
}

This gives the compile error:

Operator '<' cannot be applied to MyTemperatureObject

But I think that the compiler can use the Comparable interface to evaluate this. Or is there maybe a reason that this is not possible or not wisely to do this.

I know that I can use the compareTo function

a.compareTo(b) < 0

But I think this is better readable/understandable

 a < b
3
  • I think this is better readable/understandable a<b, is not supported in Java for MyTemperatureObject. Commented Apr 20, 2015 at 7:24
  • 1
    And in future if you get another doubt of using equals method instead of == as it is more readable/understandable, then please refer this answer. Commented Apr 20, 2015 at 7:28
  • You cannot overload operators in java ! Commented Apr 20, 2015 at 8:54

4 Answers 4

5

In Java, the < operator can only be used on numeric types. The compiler will not automagically apply the compareTo method for object comparisons.

Rewrite your condition as follows:

if (a.compareTo(b) < 0){ /* ... */ }

The semantics of the compareTo method are clearly documented in the JavaDoc, but in essence it boils down to this:

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Sign up to request clarification or add additional context in comments.

4 Comments

I think you should explain him the return value of the compareTo method as well.
I know this function I have implemented this function. But what I want is to not use this function. a < b is more understandable than a.compareTo(b) < 0
But what are the downsides for to not support this?
I don't see any immediate downsides. Dealing with null cases would probably be a little bit tricky, as well as dealing with incompatible types. All issues that could probably be solved more or less elegantly, but I can't speculate about the original language designers' motivations.
2

if you want to do like on C++ operator overloading : Wikipedia - C++ Operator Overloading, then it doesn't exist on java.

Comments

0

You can refer documentation of compareTo method.

Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

You can use compareTo method.

if (a.compareTo(b) < 0){
    // Your code
}

Operator '<' cannot be applied to MyTemperatureObject

Answer : Operator overloading is not supported in Java, as it is supported in C++. So you can use < / > operator for numeric comparison only.

Comments

0

You must use specific class that invoke compareTo method, Take a look as this tutorial, I hope help you http://www.tutorialspoint.com/java/java_using_comparator.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.