This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If enum implements Comparable so why can't compare with < or >?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable objects can be compared using the compareTo() method. < and > are only available for primitive numeric types.
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic