You cannot do operator overloading in Java. This means you are not able to define custom behaviors for operators such as +, >, <, ==, etc. in your own classes.

As you already noted, implementing Comparable and using the compareTo() method is probably the way to go in this case.

Another option is to create a Comparator (see the docs), specially if it doesn't make sense for the class to implement Comparable or if you need to compare objects from the same class in different ways.

To improve the code readability you could use compareTo() together with custom methods that may look more natural. For example:

boolean isGreaterThan(MyObject<T> that) {
    return this.compareTo(that) > 0;
}

boolean isLessThan(MyObject<T> that) {
    return this.compareTo(that) < 0;
}

Then you could use them like this:

if (obj1.isGreaterThan(obj2)) {
    // do something
}
Answer from Anderson Vieira on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › comparing objects in java
Comparing Objects in Java | Baeldung
October 10, 2025 - Let’s take the Ints helper class and see how its compare() method works: ... As usual, it returns an integer that may be negative, zero, or positive if the first argument is lesser, equal, or greater than the second, respectively.
🌐
Medium
medium.com › @AlexanderObregon › javas-objects-compare-method-explained-7a9ccda5ea74
Java’s Objects.compare() Method Explained | Medium
December 13, 2024 - The method returns an integer that represents the result of the comparison: A negative integer if a is less than b. Zero if a and b are considered equal. A positive integer if a is greater than b.
🌐
HappyCoders.eu
happycoders.eu › java › comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Objects in Java
June 12, 2025 - You compare Java primitives (int, long, double, etc.) using the operators <, <=, ==, =>, >. That does not work for objects. For objects, you either use a compare or a compareTo method instead. I will explain both methods using Strings and a self-written "Student" class as examples. ... Now we want to determine if s1 is less than, greater than, or equal to s2.
🌐
W3Schools
w3schools.com › java › java_operators_comparison.asp
Java Comparison Operators
These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else chapter. In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:
🌐
Lean Tactic Reference
course.ccs.neu.edu › cs5004 › lectureequality_and_comparison.html
Lecture 6: Equality and comparison
The Comparable<T> interface in Java contains a single method public int compareTo(T other). This method is expected to return a negative numbef if the calling object is lesser than the other object, a positive number if the calling object is greater than the other object, or 0 otherwise (if ...
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
Unlike numbers, Java doesn't allow direct comparisons using < or > because objects don’t have a built-in way to determine which one is "greater" or "smaller." That’s where compareTocomes in.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
1 week ago - This interface is a member of the Java Collections Framework. ... Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Find elsewhere
🌐
Educative
educative.io › answers › what-is-objectscompare-in-java
What is Objects.compare() in Java?
In the code snippet below, we pass the integer values as objects to the method and an integer comparator. ... If we run the program above, it will print 1, as the first integer object is greater than the second one.
Top answer
1 of 2
9

You would need to ensure that your generic type implemented the Comparable interface, and then use the compareTo method instead. Java does not support overloading the > operator (or any operator overloading, for that matter).

As per the documents, compareTo:

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

An example (that you'll have to map on to your exact code), assuming that key is your item you will store in your node, and checkCell.key is your node

int compareResult = key.compareTo(checkCell.key);
if (key < 0) { // it goes on the left }
else if (key == 0) { // it is the same }
else { // it goes on the right }

In your compareTo method you need to decide what fields in your class determine it's "ordering". For example, if you have a size and priority field, you might do:

@Override public int compareTo(Type other) {
  final int BEFORE = -1;
  final int EQUAL = 0;
  final int AFTER = 1;

  if (this == other) return EQUAL;

  if (this.size < other.size) return BEFORE;
  else if (this.size > other.size) return AFTER;
  else { // size is equal, so test priority
    if (this.priority < other.priority) return BEFORE;
    else if (this.priority > other.priority) return AFTER;
  }
  return EQUAL;
}
2 of 2
7

Bounded type parameters are key to the implementation of generic algorithms. Consider the following method that counts the number of elements in an array T[] that are greater than a specified element elem.

public static <T> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e > elem)  // compiler error
            ++count;
    return count;
}

The implementation of the method is straightforward, but it does not compile because the greater than operator (>) applies only to primitive types such as short, int, double, long, float, byte, and char. You cannot use the > operator to compare objects. To fix the problem, use a type parameter bounded by the Comparable<T> interface:

public interface Comparable<T> {
    public int compareTo(T o);
}

The resulting code will be:

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e.compareTo(elem) > 0)
            ++count;
    return count;
}

bounded type parameters

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 7 )
March 26, 2025 - This interface is a member of the Java Collections Framework. ... Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
🌐
CodingTechRoom
codingtechroom.com › question › understanding-java-syntax-greater-than-less-than-operators
Understanding Java Syntax for Greater Than and Less Than Operators: Are They Class-Specific? - CodingTechRoom
In Java, the greater than (>) and less than (<) operators are fundamental comparison operators used to compare values. While they are commonly used with primitive data types like integers and floating-point numbers, their behavior is not defined for objects unless explicitly overridden in the ...
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-a-number-is-greater-than-another-number-in-java-559963
How to Check If a Number Is Greater Than Another Number in Java | LabEx
Learn how to check if a number is greater than another in Java using the > operator. This hands-on lab covers comparing different numeric types and handling equal numbers.
🌐
Medium
medium.com › omarelgabrys-blog › comparing-objects-307400115f88
Comparing Objects. Add the Comparable flavor, and taste… | by Omar Elgabry | OmarElgabry's Blog | Medium
January 22, 2026 - The compareTo method defines the ... -1), if the current triggering object is less than the passed one, and positive integer (usually +1) if greater than, and 0 if equal....
Top answer
1 of 9
24

This is the right way to compare strings:

int studentCompare = this.lastName.compareTo(s.getLastName()); 

This won't even compile:

if (this.getLastName() < s.getLastName())

Use if (this.getLastName().compareTo(s.getLastName()) < 0) instead.

So to compare fist/last name order you need:

int d = getFirstName().compareTo(s.getFirstName());
if (d == 0)
    d = getLastName().compareTo(s.getLastName());
return d;
2 of 9
18

The compareTo method is described as follows:

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

Let's say we would like to compare Jedis by their age:

class Jedi implements Comparable<Jedi> {

    private final String name;
    private final int age;
        //...
}

Then if our Jedi is older than the provided one, you must return a positive, if they are the same age, you return 0, and if our Jedi is younger you return a negative.

public int compareTo(Jedi jedi){
    return this.age > jedi.age ? 1 : this.age < jedi.age ? -1 : 0;
}

By implementing the compareTo method (coming from the Comparable interface) your are defining what is called a natural order. All sorting methods in JDK will use this ordering by default.

There are ocassions in which you may want to base your comparision in other objects, and not on a primitive type. For instance, copare Jedis based on their names. In this case, if the objects being compared already implement Comparable then you can do the comparison using its compareTo method.

public int compareTo(Jedi jedi){
    return this.name.compareTo(jedi.getName());
}

It would be simpler in this case.

Now, if you inted to use both name and age as the comparison criteria then you have to decide your oder of comparison, what has precedence. For instance, if two Jedis are named the same, then you can use their age to decide which goes first and which goes second.

public int compareTo(Jedi jedi){
    int result = this.name.compareTo(jedi.getName());
    if(result == 0){
        result = this.age > jedi.age ? 1 : this.age < jedi.age ? -1 : 0;
    }
    return result;
}

If you had an array of Jedis

Jedi[] jediAcademy = {new Jedi("Obiwan",80), new Jedi("Anakin", 30), ..}

All you have to do is to ask to the class java.util.Arrays to use its sort method.

Arrays.sort(jediAcademy);

This Arrays.sort method will use your compareTo method to sort the objects one by one.

🌐
LabEx
labex.io › tutorials › java-how-to-compare-java-objects-based-on-multiple-attributes-417392
How to compare Java objects based on multiple attributes | LabEx
The method returns: A negative integer if the current object is less than the other object · Zero if the current object is equal to the other object · A positive integer if the current object is greater than the other object
🌐
Belief Driven Design
belief-driven-design.com › equality-and-comparison-in-java-a5e0f05b808
Equality and Comparison in Java: Pitfalls and Best Practices | belief driven design
January 15, 2020 - Always use boolean equals(Object other) to compare for equality. To make a type testable for equality, we need to implement both equals and hashCode to ensure correct and consistent behavior. Always remember that floating-point data types aren’t exact and are harder to compare.
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Comparing_Objects
Comparing Objects - Wikibooks, open books for an open world
So when you define a new class and want the objects of your class to be sortable, you have to implement the Comparable and redefine the compareTo(Object obj) method. ... A negative integer means that the current object is before the parameter object in the natural ordering. Zero means that the current object and the parameter object are equal. A positive integer means that the current object is after the parameter object in the natural ordering. Let's say that the name is more important than the address and the description is ignored.
🌐
University of Edinburgh
homepages.inf.ed.ac.uk › wadler › gj › doc › java.lang.Comparable.html
public interface java.lang.Comparable<A>
Compares this Object with the specified Object for order. Returns a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.
🌐
Medium
medium.com › @dr4wone › equals-vs-compareto-in-java-understanding-the-differences-fce0a0d4b292
Equals vs. compareTo in Java: Understanding the Differences | by Daniel Baumann | Medium
April 9, 2023 - The compareTo() method is used to compare objects based on their natural ordering. It returns a negative integer, zero, or a positive integer, depending on whether the current object is less than, equal to, or greater than the object passed ...