Sub topic 7
Use Arrow-Style Cases That Do Not Fall Through
An arrow-style switch statement picks an action based on one value. The value it checks is called the selector. Each case label lists a value that can match the selector. The arrow connects the label to the action on its right. Together, the label and its action form a rule.
The main benefit of the arrow is that you do not need to remember a break. In a colon-style switch, code can keep running into the next case. That is called fall-through. In an arrow-style switch, Java runs only the selected rule. When that rule finishes, the program continues after the switch.
Run a Switch with One Action per Rule
This program reads a command from a variable. A variable is a name that holds a value. Here, command holds a String, which is Java’s type for text.
Save the program as ArrowSwitchDemo.java. The file name must match the name after public class.
public class ArrowSwitchDemo {
public static void main(String[] args) {
String command = "help";
switch (command) {
case "help" -> System.out.println("Show available commands");
case "quit" -> System.out.println("Goodbye");
default -> System.out.println("Unknown command");
}
System.out.println("Done checking the command");
}
}Open a terminal in the folder where you saved the file. A terminal is a window where you type commands. The first command builds the program. The second runs it.
javac ArrowSwitchDemo.java
java ArrowSwitchDemoYou should see these two lines:
Show available commands
Done checking the commandTrace the Match
- Java reads command, the selector inside the parentheses. Its value is “help”.
- The case labeled “help” matches, so Java prints “Show available commands”.
- Java skips the “quit” and default actions. Neither can run after the “help” rule.
- Java reaches the print line after the switch. The arrow ends the rule, not the whole program.
Change the value of command and run both terminal commands again. Rebuilding matters: if you only run the second command, you may run the old version.
| Value of command | First line printed | Why |
|---|---|---|
| “help” | Show available commands | The help rule matches. |
| “quit” | Goodbye | The quit rule matches. |
| “Help” | Unknown command | An uppercase H does not match a lowercase h. |
| “start” | Unknown command | No case matches, so Java runs default. |
The default rule is the fallback. Java uses it only when no case matches. In this example, you could leave out default. Then a command like “start” would print nothing inside the switch, but the final “Done checking the command” line would still print. Keeping default gives the user feedback when the program does not know the command.
Group Values and Run More Than One Action
Sometimes two inputs should do the same thing. For example, a music player might accept both “p” and “play”. Put those values in the same case label, with a comma between them. Either value can select that rule.
To run several actions in one rule, put them inside braces. A block is a group of lines enclosed in braces. The closing brace marks the end of the rule’s action. You still do not add a break.
Save this program as PlayerSwitchDemo.java. It prints what a player would do, but it does not play any audio.
public class PlayerSwitchDemo {
public static void main(String[] args) {
String command = "p";
switch (command) {
case "p", "play" -> {
System.out.println("Starting playback");
System.out.println("Enjoy the music");
}
case "pause" -> System.out.println("Playback paused");
default -> System.out.println("Unknown player command");
}
}
}javac PlayerSwitchDemo.java
java PlayerSwitchDemoThe value “p” selects the first rule. Java prints “Starting playback” and then “Enjoy the music”. It does not print “Playback paused”. Try “play” next. Predict the output before you rebuild and run the program: both inputs select the same block.
Avoid Two Common Mistakes
- If you add a second print line to a rule, wrap both print lines in braces. Putting the second line below the first does not make it part of the rule.
- Use arrows for all rules in the same switch. Java does not let you mix colon-style labels and arrow-style labels in one switch.
Check Your Understanding
Exercise
In ArrowSwitchDemo, you change command to “quit”, rebuild, and run it. Which two lines print, and why does “Unknown command” not appear?
Show answer
“Goodbye” prints first, then “Done checking the command”. The quit rule matches, so Java skips default and continues after the switch.
Exercise
How would you let PlayerSwitchDemo accept “pause” or “hold” for the same action?
Show answer
Add "hold" after "pause" in the same case label, separated by a comma. Keep the one arrow and the existing print call. Either value now selects that rule.