I'm currently working on a University assignment. I have a piece of code that looks like the following.
if(temporary > left) { }
The temporary and left variables are both of the "Comparable" type. My question is, how can I compare these two? They always hold integer values, but the assignment forces me to declare them as "Comparable" types.
Any help is much appreciated as I'm quite confused with this. Thanks!
If you have a look at http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html, you will know that the compareTo()
method is what you need.
You can use temporary.compareTo(left)
and use the value to compare with 0
.
Implement compareTo()
in temporarytemporary.compareTo(left)
such that it returns a negative integer, zero, or a positive integer as temporary is less than, equal to, or greater than the left.