Bad Operand Types for Binary Operator “>”?

Sure – you simply can’t apply the > operator between objects. What would you expect it to do? You can’t apply any of the other binary operators either – +-/ etc (with the exception of string concatenation).

Ideally, you should make your TreeNode generic, and either have a Comparator<T> which is able to compare any two instances, or make T extend Comparable<T>. Either way, you can then compare them with:

int comparisonResult = comparator.compare(current.getValue(), t.getValue());
if (comparisonResult > 0) {
  // current "greater than" t
} else if (comparisonResult < 0) {
  // current "less than" t
} else {
  // Equal
}

or

int comparisonResult = current.getValue().compareTo(t.getValue());
// Code as before

Without generics you could cast the values to Comparable or still use a general Comparator… but generics would be a better bet.

Leave a Comment