Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.

Similar presentations


Presentation on theme: "Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants."— Presentation transcript:

1 Enumeration Types Managing Ordered Sets

2 Enumeration types are designed to increase the readability of programs Used to define a set of named constants that can be used instead of numbers in a program Such as days of week, months, suits in a deck of cards Months are often set as 1-12 To input or use these values often requires the conversion back and forth between the numeric value and the string value. Enums allow us to refer to a set of objects by name instead of using numbers

3 Declaring enums Part of the Java.lang package so nothing to import Syntax – enum EnumName { obj1,obj2,obj3}; where these are the names of the constant objects enum Weekday { Sun, Mon, Tues, Weds, Thurs, Fri, Sat }; When executed, an object is instantiated for each name in the list, of the type “Weekday”

4 Using enum Enums are constant objects – they cannot be changed. References are by the dot syntax Weekday.Sun Weekday w; w = Weekday.Thurs;

5 Methods Int compareTo( Enum obj) Compares two enum objects and returns Negative value if this object is less than the argument Positive value if this object is more than the argument Zero if the two objects are the same

6 Boolean equals ( Enum obj) Returns true if this object is equal to the argument Int ordinal() Returns the numeric value of the enum object String toString() Returns the name of the enum constant Enum valueOf ( String enumName) Static method that returns the enum object whose name is the same as the String argument


Download ppt "Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants."

Similar presentations


Ads by Google