Enumerations
This page is under construction. Please come back later.
import java.util.HashMap; import java.util.Map; import org.pureprogrammer.Utils; public class Enums1 { enum CompassPoint { NORTH, EAST, SOUTH, WEST } static String CompassPointToString(final CompassPoint c) { String result = ""; switch (c) { case NORTH: result = "North"; break; case EAST: result = "East"; break; case SOUTH: result = "South"; break; case WEST: result = "West"; break; } return result; } static CompassPoint turnRight(final CompassPoint c) { if (c == CompassPoint.NORTH) { return CompassPoint.EAST; } else if (c == CompassPoint.EAST) { return CompassPoint.SOUTH; } else if (c == CompassPoint.SOUTH) { return CompassPoint.WEST; } else { return CompassPoint.NORTH; } } public static void main(String[] args) { CompassPoint cp1 = CompassPoint.NORTH; System.out.println("cp1: " + cp1); System.out.println("SOUTH: " + CompassPointToString(CompassPoint.SOUTH)); System.out.println("turnRight(cp1): " + CompassPointToString(turnRight(cp1))); CompassPoint cp2 = CompassPoint.EAST; if (cp1 == cp2) { System.out.println("cp1 == cp2"); } else { System.out.println("cp1 != cp2"); } cp1 = cp2; if (cp1 == cp2) { System.out.println("cp1 == cp2"); } else { System.out.println("cp1 != cp2"); } } }
Output
$ javac -Xlint Enums1.java
$ java -ea Enums1
cp1: NORTH
SOUTH: South
turnRight(cp1): East
cp1 != cp2
cp1 == cp2
import java.util.HashMap; import java.util.Map; import org.pureprogrammer.Utils; public class Enums2 { enum Color { RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6); private static Map<Integer, Color> byValue = new HashMap<>(); static { for (Color e: values()) { byValue.put(e.value, e); } } public final int value; private Color(int v) { value = v; } public int value() { return value; } public static Color valueOf(int i) { Color result = byValue.get(i); return result; } } static Color combineColors(final Color c1, final Color c2) { final int i1 = c1.value(); final int i2 = c2.value(); final int result = i1 | i2; final Color returnColor = Color.valueOf(result); return returnColor; } public static void main(String[] args) { Color c1 = Color.BLUE; System.out.println("c1: " + c1); System.out.println("RED: " + Color.RED); System.out.println("combineColors(c1, RED): " + combineColors(c1, Color.RED)); Color c2 = Color.GREEN; System.out.println("combineColors(c1, c2): " + combineColors(c1, c2)); if (c1 == c2) { System.out.println("c1 == c2"); } else { System.out.println("c1 != c2"); } c1 = c2; if (c1 == c2) { System.out.println("c1 == c2"); } else { System.out.println("c1 != c2"); } Color bad = Color.valueOf(7); System.out.println("bad: " + bad); } }
Output
$ javac -Xlint Enums2.java
$ java -ea Enums2
c1: BLUE
RED: RED
combineColors(c1, RED): MAGENTA
combineColors(c1, c2): CYAN
c1 != c2
c1 == c2
bad: null
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Java Language Specification]], Java SE 17 Edition, Gosling, et. al., 2021.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]