A Different Kind of Variable Enumerations A Different Kind of Variable Enumerations appear in Java first in Java 5 They are unlike C++ because they are a class. Copyright © 2005-2016 Curt Hill
Conventions There are many times when you use a code that represents a value Many times there are existing conventions for a code and its meaning For example months When we say that today is 3/20 you know that the 3 represents March An enumeration allows us to name these in a way that makes the program more readable Copyright © 2005-2016 Curt Hill
The Problem More often there are no existing conventions Programmer makes up the convention What kind of convention? Choices: Strings Chars Ints Copyright © 2005-2016 Curt Hill
Example Consider classifying students such as freshmen, sophomores, juniors, seniors, graduate, prospects, extension We could create a code to represent these in a file that described our students We would not want to type in the entire classification We could misspell it and cause our extraction of juniors not to work Takes too much space Copyright © 2005-2016 Curt Hill
Example continued We will usually assign a letter, unless there are too many or they start with same letter Sophomore and senior Then we use a number So 1=freshman, 2=sophomore ... The problem with that is we will see in our code statements like: s = 6 The reader then wonders what 6 represents Copyright © 2005-2016 Curt Hill
One Solution One approach to solve this is to use named constants: final int freshman=1,sophomore=2…; Another solution to this problem is to use an enumeration type Copyright © 2005-2016 Curt Hill
Enumeration Type Enumerations are a shortened way to make a series of named constants In this type we make up a series of names that we will call our items as a new type enum student_class {freshman, sophomore, junior, senior, grad, prospect, extension}; What we are saying is that we want a new type that has precisely 7 values We have not yet defined a variable Copyright © 2005-2016 Curt Hill
Code Now we can use these in sample code enum Directions {LEFT, FORWARD, RIGHT,BACKWARD}; … Directions dir = Directions.LEFT; Directions is now a type It is usually placed with constants Copyright © 2005-2016 Curt Hill
C++ and Java In C++ an enumeration is a series of named constants In Java it is actually a special form of a class This class does have some methods defined These methods need: import java.lang.Enum; Copyright © 2005-2016 Curt Hill
Enumeration Methods The method ordinal gives the ordinal position Starts at zero dir.ordinal() == 0 indicates that this is LEFT The toString method gives the string representation valueOf takes a string and gives the enumeration value It must be an exact match otherwise IllegalArgumentException is thrown Copyright © 2005-2016 Curt Hill
Comparisons equals gives an equality comparison However the == is preferable compareTo gives -1 if less, 0 if equal and +1 if greater The other comparison operators are not allowed Copyright © 2005-2016 Curt Hill
Representation of value Although technically it is supposed to be hidden from us Practically we do know it Each value in the list is stored as an integer First item is zero Count up from there Copyright © 2005-2016 Curt Hill
Booleans Booleans are a predefined enumeration with special privileges Two values are {false, true} The special privileges include the being result type of all comparisons The enum reserved word is not needed Copyright © 2005-2016 Curt Hill
Declaration note Enumerations are always declared enclosed in braces The Java convention is that like constants they are usually all in upper case This is just a convention Note that bools use lower case for their constants Copyright © 2005-2016 Curt Hill
Constants When using an enumeration constant, it must be prefixed by the enumeration type Directions dir = Directions.LEFT; The exception to this rule is the case constant in a switch: switch(dir){ case LEFT: Copyright © 2005-2016 Curt Hill
Finally Enumerations are very handy for naming constants that are related They tend to make use of such constants more readable They are relatively new feature From Java 5.0 enum was a reserved word from Java 1.0 but not used Copyright © 2005-2016 Curt Hill