Using Strings as case Constants
Example 4.4 illustrates using strings in a switch statement. The thing to note is what constitutes a constant string expression that can be used as a case constant. The case constants at (3), (4), (5), and (6) are all valid constant string expressions, as the compiler can figure out their value at compile time. String literals, used at (3) and (6), and constant field values, declared at (1) and (2a) and used at (4) and (5), are all valid case constants. In contrast, the HOT reference from declarations (2b) and (2c) cannot be used as a case constant. From the declaration at (2a), the compiler cannot guarantee that the value of the reference will not change at runtime. From the declaration at (2c), it cannot deduce the value at compile time, as the constructor must be run to construct the value.
Switching on strings is essentially based on equality comparison of integer values that are hash values of strings, followed by an object equality test to rule out the possibility of collision between two different strings having the same hash value. Switching on strings should be used judiciously, as it is less efficient than switching on integers. Switching on strings is not advisable if the values being switched on are not already strings.
Example 4.4 Strings in a switch Statement
public class SwitchingOnAString {
public static final String MEDIUM = “Medium”; // (1)
public static final String HOT = “Hot”; // (2a)
//public static String HOT = “Hot”; // (2b) Not OK as case label
//public static final String HOT = new String(“Hot”); // (2c) Not OK as case label
public static void main(String[] args) {
String spiceLevel = “Medium_Hot”;
switch (spiceLevel) {
case “Mild”, // (3)
MEDIUM + “_” + HOT -> System.out.println(“Enjoy your meal!”); // (4)
case HOT -> System.out.println(“Have fun!”); // (5)
case “Suicide” -> System.out.println(“Good luck!”); // (6)
default -> System.out.println(“You being funny?”);
}
}
}
Output from the program:
Enjoy your meal!
Using Enum Constants as case Constants
Example 4.5 illustrates the use of enum types (ยง5.13, p. 287) in a switch statement with the arrow notation. The enum type SpiceGrade is defined at (1). The type of the selector expression at (2) is the enum type SpiceGrade. Note that the enum constants are not specified with their fully qualified name (see (3a)). Using the fully qualified name results in a compile-time error, as shown at (3b). Only enum constants that have the same enum type as the selector expression can be specified as case label values.
The semantics of the switch statement are the same as described earlier. Switching on enum values is essentially based on equality comparison of unique integer values that are ordinal values assigned by the compiler to the constants of an enum type.
When the switch rules cover all values of the selector expression type, the switch statement is said to be exhaustive. Non-exhaustive switch statements are a common cause of programming errors. It is up to the programmer to ensure that the switch statement is exhaustive, as the compiler does not provide any help in this regard for the switch statement. Judicious use of the default label should be considered, as illustrated in the examples provided in this section that use the switch statement.
Example 4.5 Enums in a switch Statement
enum SpiceGrade { MILD, MEDIUM, MEDIUM_HOT, HOT, SUICIDE; } // (1)
public class SwitchingFun {
public static void main(String[] args) {
SpiceGrade spicing = SpiceGrade.HOT;
switch (spicing) { // (2)
case HOT -> System.out.println(“Have fun!”); // (3a) OK!
// case SpiceGrade.HOT // (3b) Compile-time error!
// -> System.out.println(“Have fun!”);
case SUICIDE -> System.out.println(“Good luck!”);
default -> System.out.println(“Enjoy your meal!”);
}
}
}
Output from the program:
Have fun!