Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Different Kind of Variable

Similar presentations


Presentation on theme: "A Different Kind of Variable"— Presentation transcript:

1 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 © Curt Hill

2 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 © Curt Hill

3 The Problem More often there are no existing conventions
Programmer makes up the convention What kind of convention? Choices: Strings Chars Ints Copyright © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 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 © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill


Download ppt "A Different Kind of Variable"

Similar presentations


Ads by Google