SlideShare a Scribd company logo
1
CSE 331
Enumerated types (enum)
slides created by Marty Stepp
based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
http://www.cs.washington.edu/331/
2
Anti-pattern: int constants
public class Card {
public static final int CLUBS = 0;
public static final int DIAMONDS = 1;
public static final int HEARTS = 2;
public static final int SPADES = 3;
...
private int suit;
...
public void setSuit(int suit) {
this.suit = suit;
}
}
• What's wrong with using int constants to represent card suits?
 variation (also bad): using Strings for the same purpose.
3
Enumerated types
• enum: A type of objects with a fixed set of constant values.
public enum Name {
VALUE, VALUE, ..., VALUE
}
• Usually placed into its own .java file.
• C has enums that are really ints; Java's are objects.
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
• Effective Java Tip #30: Use enums instead of int constants.
"The advantages of enum types over int constants are compelling.
Enums are far more readable, safer, and more powerful."
4
What is an enum?
• The preceding enum is roughly equal to the following short class:
public final class Suit extends Enum<Suit> {
public static final Suit CLUBS = new Suit();
public static final Suit DIAMONDS = new Suit();
public static final Suit HEARTS = new Suit();
public static final Suit SPADES = new Suit();
private Suit() {} // no more can be made
}
5
What can you do with an enum?
• use it as the type of a variable, field, parameter, or return
public class Card {
private Suit suit;
...
}
• compare them with == (why don't we need to use equals?)
if (suit == Suit.CLUBS) { ...
• compare them with compareTo (by order of declaration)
public int compareTo(Card other) {
if (suit != other.suit) {
return suit.compareTo(other.suit);
} ...
}
6
The switch statement
switch (boolean test) {
case value:
code;
break;
case value:
code;
break;
...
default: // if it isn't one of the above values
code;
break;
}
• an alternative to the if/else statement
 must be used on integral types (e.g. int, char, long, enum)
 instead of a break, a case can end with a return, or if neither is
present, it will "fall through" into the code for the next case
7
Enum methods
method description
int compareTo(E) all enum types are Comparable by order of
declaration
boolean equals(o) not needed; can just use ==
String name() equivalent to toString
int ordinal() returns an enum's 0-based number by order
of declaration (first is 0, then 1, then 2, ...)
method description
static E valueOf(s) converts a string into an enum value
static E[] values() an array of all values of your enumeration
8
EnumSet
• class EnumSet from java.util represents a set of enum values
and has useful methods for manipulating enums:
Set<Coin> coins = EnumSet.range(Coin.NICKEL, Coin.QUARTER);
for (coin c : coins) {
System.out.println(c); // see also: EnumMap
}
 Effective Java Tip #32: Use EnumSet instead of bit fields.
 Effective Java Tip #33: Use EnumMap instead of ordinal indexing.
static EnumSet<E> allOf(Type) a set of all values of the type
static EnumSet<E> complementOf(set) a set of all enum values other
than the ones in the given set
static EnumSet<E> noneOf(Type) an empty set of the given type
static EnumSet<E> of(...) a set holding the given values
static EnumSet<E> range(from, to) set of all enum values declared
between from and to
9
More complex enums
• An enumerated type can have fields, methods, and constructors:
public enum Coin {
PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private int cents;
private Coin(int cents) {
this.cents = cents;
}
public int getCents() { return cents; }
public int perDollar() { return 100 / cents; }
public String toString() { // "NICKEL (5c)"
return super.toString() + " (" + cents + "c)";
}
}

More Related Content

PPT
enums
PPTX
Lecture 6 Enumeration in java ADVANCE.pptx
PPTX
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
PPTX
Java essence part 1
PPT
Effective Java - Enum and Annotations
PPTX
Java enum
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPT
MODULE 1 EnumerationSSP. for adavanced lecture
enums
Lecture 6 Enumeration in java ADVANCE.pptx
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
Java essence part 1
Effective Java - Enum and Annotations
Java enum
Enumeration in Java Explained | Java Tutorial | Edureka
MODULE 1 EnumerationSSP. for adavanced lecture

Similar to 05a-enum.ppt (20)

ODP
Enum - Coding Guidelines
PPTX
C programming enumeration
PPTX
Enumerated data types
PDF
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
PPTX
PPTX
Enumerations in java.pptx
PDF
c# Enumerations
PDF
ENUM - make u r names as data types
PPTX
Java New Programming Features
PPTX
8enum in c#
PPTX
Enumeration In JAVA
PPTX
Enumration datatype
PPT
Jdk1.5 Features
PPT
Structure and Enum in c#
PDF
Evolution and Examples of Java Features, from Java 1.7 to Java 22
PDF
Tiger: Java 5 Evolutions
PPTX
PDF
PDF
Evolution and Examples of Java Features, from Java 1.7 to Java 24
PDF
Download IObit Driver Booster Pro Crack Latest Version [Updated]
Enum - Coding Guidelines
C programming enumeration
Enumerated data types
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Enumerations in java.pptx
c# Enumerations
ENUM - make u r names as data types
Java New Programming Features
8enum in c#
Enumeration In JAVA
Enumration datatype
Jdk1.5 Features
Structure and Enum in c#
Evolution and Examples of Java Features, from Java 1.7 to Java 22
Tiger: Java 5 Evolutions
Evolution and Examples of Java Features, from Java 1.7 to Java 24
Download IObit Driver Booster Pro Crack Latest Version [Updated]
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
history of c programming in notes for students .pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Cost to Outsource Software Development in 2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Design an Analysis of Algorithms I-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
history of c programming in notes for students .pptx
Transform Your Business with a Software ERP System
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
Which alternative to Crystal Reports is best for small or large businesses.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo Companies in India – Driving Business Transformation.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Wondershare Filmora 15 Crack With Activation Key [2025
Understanding Forklifts - TECH EHS Solution
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
Odoo POS Development Services by CandidRoot Solutions
Cost to Outsource Software Development in 2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Design an Analysis of Algorithms I-SECS-1021-03
Ad

05a-enum.ppt

  • 1. 1 CSE 331 Enumerated types (enum) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/
  • 2. 2 Anti-pattern: int constants public class Card { public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3; ... private int suit; ... public void setSuit(int suit) { this.suit = suit; } } • What's wrong with using int constants to represent card suits?  variation (also bad): using Strings for the same purpose.
  • 3. 3 Enumerated types • enum: A type of objects with a fixed set of constant values. public enum Name { VALUE, VALUE, ..., VALUE } • Usually placed into its own .java file. • C has enums that are really ints; Java's are objects. public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } • Effective Java Tip #30: Use enums instead of int constants. "The advantages of enum types over int constants are compelling. Enums are far more readable, safer, and more powerful."
  • 4. 4 What is an enum? • The preceding enum is roughly equal to the following short class: public final class Suit extends Enum<Suit> { public static final Suit CLUBS = new Suit(); public static final Suit DIAMONDS = new Suit(); public static final Suit HEARTS = new Suit(); public static final Suit SPADES = new Suit(); private Suit() {} // no more can be made }
  • 5. 5 What can you do with an enum? • use it as the type of a variable, field, parameter, or return public class Card { private Suit suit; ... } • compare them with == (why don't we need to use equals?) if (suit == Suit.CLUBS) { ... • compare them with compareTo (by order of declaration) public int compareTo(Card other) { if (suit != other.suit) { return suit.compareTo(other.suit); } ... }
  • 6. 6 The switch statement switch (boolean test) { case value: code; break; case value: code; break; ... default: // if it isn't one of the above values code; break; } • an alternative to the if/else statement  must be used on integral types (e.g. int, char, long, enum)  instead of a break, a case can end with a return, or if neither is present, it will "fall through" into the code for the next case
  • 7. 7 Enum methods method description int compareTo(E) all enum types are Comparable by order of declaration boolean equals(o) not needed; can just use == String name() equivalent to toString int ordinal() returns an enum's 0-based number by order of declaration (first is 0, then 1, then 2, ...) method description static E valueOf(s) converts a string into an enum value static E[] values() an array of all values of your enumeration
  • 8. 8 EnumSet • class EnumSet from java.util represents a set of enum values and has useful methods for manipulating enums: Set<Coin> coins = EnumSet.range(Coin.NICKEL, Coin.QUARTER); for (coin c : coins) { System.out.println(c); // see also: EnumMap }  Effective Java Tip #32: Use EnumSet instead of bit fields.  Effective Java Tip #33: Use EnumMap instead of ordinal indexing. static EnumSet<E> allOf(Type) a set of all values of the type static EnumSet<E> complementOf(set) a set of all enum values other than the ones in the given set static EnumSet<E> noneOf(Type) an empty set of the given type static EnumSet<E> of(...) a set holding the given values static EnumSet<E> range(from, to) set of all enum values declared between from and to
  • 9. 9 More complex enums • An enumerated type can have fields, methods, and constructors: public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); private int cents; private Coin(int cents) { this.cents = cents; } public int getCents() { return cents; } public int perDollar() { return 100 / cents; } public String toString() { // "NICKEL (5c)" return super.toString() + " (" + cents + "c)"; } }