aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can't we compare two enum values with '<'? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can Watch "How can New topic
Author

How can't we compare two enum values with '<'?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

If enum implements Comparable so why can't compare with < or >?

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4244
    
    7

Comparable objects can be compared using the compareTo() method. < and > are only available for primitive numeric types.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86


Assuming that BIGGEST was declared after BIG in the enumeration. The ordinal value in an enumeration is implicitly tied to the declaration order, by default the first value is assigned value 0, the second value 1 and so on.

So if yo declared the enumeration like this, things will work:



----

Implementing Comparable doesn't mean that you can use < or >. You can only use those with numeric values.

It only means that there's a compareTo() method. Try this:


This method will return an int that is smaller than, equal to or bigger than 0, depending on which value is "bigger". In the case of enum values the "size" depends on the order of the enum value definitions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can't we compare two enum values with '<'?
 
Similar Threads
wrapper class
Autoboxing / Unboxing doubt
What this code does
String Test
Descending order and associating Array Elements in sort method to another's elements