switch with arrows (->)
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Since Java 14, a switch can use arrows: case 1 -> .... An arrow case runs only its own code, so there is no break and no fall-through, and the whole switch can give back a value.
Arrow cases: no break, no fall-through
The fall-through bug in The switch statement (opens in a new tab) comes from one forgotten break. An arrow case, case value -> ..., runs only its own code and then leaves the switch.
Example 1 · Arrow cases with a block
public class DayType { public static void main(String[] args) { int day = 6; switch (day) { case 1, 2, 3, 4, 5 -> System.out.println("College day"); case 6, 7 -> { System.out.println("Holiday"); System.out.println("Library open 10 to 2"); } default -> System.out.println("Invalid day"); } }}Holiday Library open 10 to 2
What this code does
case 6, 7matches both weekend days. Its two lines sit in braces.- Only that case runs, then the switch ends: no
breakis needed, anddefaultis not reached.
Java 17 and 21, the usual versions today, both support arrows.
A switch that gives a value
A recharge app needs the price of plan 1, 2 or 3 in a variable. A switch expression gives back one value, the way ?: does, so you store it straight away: int price = switch (plan) { ... };.
Example 2 · Store the value of a switch
import java.util.Scanner;
public class Recharge { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Plan (1-3): "); int plan = sc.nextInt(); int price = switch (plan) { case 1 -> 199; case 2 -> 299; case 3 -> 479; default -> 0; }; System.out.println("Plan " + plan + ": Rs " + price); sc.close(); }}Plan (1-3): 2 Plan 2: Rs 299
What this code does
- The switch gives back
299for plan 2, and that value goes intoprice. - Note the
};at the end: the whole switch is the right side of one assignment.
With the old switch you would write price = 199; break; in every case, and Java would still worry that price might be empty.
Cover every value with default
What price should plan 9 get? A switch expression must give a value for every possible input, so Java insists on an answer.
int plan = 9;int price = switch (plan) { // error: the switch expression does not case 1 -> 199; // cover all possible input values case 2 -> 299;};For int, char and String you cannot list every value, so add default ->. Without it, Java stops with the switch expression does not cover all possible input values. Every arrow must also give the same type: all text for a String, all numbers for an int.
Example 3 · char cases that share an answer
public class Feedback { public static void main(String[] args) { char grade = 'B'; String remark = switch (grade) { case 'A', 'B' -> "Well done"; case 'C' -> "Can improve"; default -> "Meet your mentor"; }; System.out.println(grade + ": " + remark); }}B: Well done
What this code does
case 'A', 'B'gives the same remark for both grades.- The result is a
String, so every arrow,defaultincluded, points to text.
Next step
Check your understanding
7 quick questions