Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

switch with arrows (->)

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.

Syntax
switch (value) {
case value1, value2 -> statement;
case value3 -> { statements }
default -> statement;
}

Example 1 · Arrow cases with a block

DayType.javaJava
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");        }    }}
Output
Holiday
Library open 10 to 2

What this code does

  • case 6, 7 matches both weekend days. Its two lines sit in braces.
  • Only that case runs, then the switch ends: no break is needed, and default is 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) { ... };.

plan is 2. The switch expression finds the case 2 -> 299 and gives back the value 299, which is stored in the variable price.

Example 2 · Store the value of a switch

Recharge.javaJava
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();    }}
Sample output
Plan (1-3): 2
Plan 2: Rs 299

What this code does

  • The switch gives back 299 for plan 2, and that value goes into price.
  • 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.

Java
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

Feedback.javaJava
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);    }}
Output
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, default included, points to text.

Next step

Check your understanding

7 quick questions