Sub topic 5
Match a Selector Value to a case or default
A switch statement chooses what code to run based on one value. Think of a menu with two choices: 1 for Start and 2 for Settings. When the choice is 2, the program should open Settings. If the choice is not on the menu, the program can show a message instead.
In a switch, the value you check is called the selector. A case label names a value that the selector can match. The default label marks what to run if no case label matches.
Read the Parts of a switch
Save this program as MenuDemo.java and run it. The examples in this chapter use the arrow form of switch, which is available in Java 14 and later.
public class MenuDemo {
public static void main(String[] args) {
int menuChoice = 2;
switch (menuChoice) {
case 1 -> System.out.println("Start");
case 2 -> System.out.println("Settings");
default -> System.out.println("Unknown choice");
}
System.out.println("Menu check complete");
}
}Start with the first line of the switch, not the first case. The parentheses after switch hold the selector. Here, Java reads menuChoice once and gets the value 2. The braces hold the choices. Inside the braces, case 1 names the value 1, and case 2 names the value 2. The arrow separates each label from the action to run.
Java matches 2 to case 2, so the program prints “Settings.” It does not run the action for case 1 or default. Then it moves past the closing brace of the switch and prints “Menu check complete.” That last line is outside the switch, so it is not part of any choice.
Predict the Match Before You Run
Change only the number in the line that sets menuChoice. Try 1, 2, and 9 in separate runs. Before each run, find the matching label and predict the first line of output.
| Value of menuChoice | Selected label | First line printed |
|---|---|---|
| 1 | case 1 | Start |
| 2 | case 2 | Settings |
| 9 | default | Unknown choice |
Notice that 9 still leads to an action, even though there is no case 9. That is the job of default. It is not a label for one special number. It handles any number that has no matching case in this switch.
For this menu, you may leave out default. Try removing the default line and running the program with menuChoice set to 9. The switch prints nothing, but the program still prints “Menu check complete.” If you want people to know that their choice was not on the menu, keep default and give them a clear message.
Match Text as Well as Numbers
A selector can also be a String, which is a value that holds text. With a String selector, put quotes around the text in each case label. Save this example as RoomDemo.java.
public class RoomDemo {
public static void main(String[] args) {
String room = "lab";
switch (room) {
case "lab" -> System.out.println("Go to room 12");
case "hall" -> System.out.println("Go to room 3");
default -> System.out.println("Room not found");
}
}
}As written, the program prints “Go to room 12.” Now change the value of room to “Lab” by making the first letter uppercase inside the quotes. The program prints “Room not found.” The text must match exactly. Uppercase and lowercase letters differ, and a space inside the quotes counts as part of the text. So “lab ”, with a space at the end, also goes to default.
Check the Labels When a Match Surprises You
First, check what value the selector actually holds. In RoomDemo, changing the message that a case prints does not change which text the case matches. To change the match, edit the label before the arrow.
Next, check the type. A type tells Java what kind of value it is working with. In MenuDemo, 2 is an int, a whole number. Putting quotes around it makes it text, so case "2" cannot replace case 2 in that switch. In RoomDemo, the labels need to name text values.
Finally, give each value only one label in the same switch. If you add another case 2 to MenuDemo, Java reports an error before the program can run. To change what happens for 2, edit the action in the case you already have.
Exercise
In MenuDemo, menuChoice is 7 and the default line is present. Which two lines print, and in what order?
Show answer
First “Unknown choice,” because no case matches 7. Then “Menu check complete,” because that line is outside the switch.
Exercise
Add a choice for 3 to MenuDemo that prints “Quit.” What line should you add, and how can you check it?
Show answer
Add case 3 -> System.out.println("Quit"); inside the switch. Set menuChoice to 3 and run the program. It should print “Quit” and then “Menu check complete.”