Java switch case

switch 语法、case 穿透、字符串匹配与 Java 14 箭头语法。

Beginner
Key Points忘了 break 会「穿透」执行后面的 case;Java 7 起支持 String,Java 14 起可用 `->` 免 break 并返回值。

switch 语法

switch jumps to the matching case and runs until break or the end. The expression accepts int, char, String, enums and similar — not long or floating point.

Java
public class SwitchDemo {
    public static void main(String[] args) {
        char grade = 'C';

        switch (grade) {
            case 'A':
                System.out.println("Excellent!");
                break;
            case 'B':
            case 'C':
                System.out.println("Well done");
                break;
            case 'D':
                System.out.println("You passed");
                break;
            default:
                System.out.println("Invalid grade");
        }
        System.out.println("Your grade is " + grade);
    }
}
Output
Well done
Your grade is C

case 穿透

Without break, execution falls through into the next case. Sometimes that is intentional (several values sharing logic), but usually it is a bug.

Java
public class FallThrough {
    public static void main(String[] args) {
        int day = 2;

        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("weekday");
                break;
            case 6:
            case 7:
                System.out.println("weekend");
                break;
            default:
                System.out.println("invalid");
        }

        switch (day) {
            case 1:
                System.out.print("one ");
            case 2:
                System.out.print("two ");
            case 3:
                System.out.print("three ");
        }
        System.out.println();
    }
}
Output
weekday
two three 

The second switch matches day == 2, so it enters at case 2 and case 1's one never runs; from there it falls through into case 3, printing only two and three. Change the label to case 2 to get one two three.

字符串 switch

Since Java 7 switch accepts String, comparing via hashCode plus equals. A null expression throws NullPointerException, so guard it first.

Java
public class StringSwitch {
    public static void main(String[] args) {
        String cmd = "start";

        switch (cmd) {
            case "start":
                System.out.println("starting...");
                break;
            case "stop":
                System.out.println("stopping...");
                break;
            default:
                System.out.println("unknown command");
        }
    }
}
Output
starting...

Java 14 箭头语法

case X -> stmt never falls through and needs no break; wrap several statements in braces. The whole switch can be an expression assigned to a variable, with yield returning from a block.

Java
public class ArrowSwitch {
    public static void main(String[] args) {
        int day = 6;

        switch (day) {
            case 1, 2, 3, 4, 5 -> System.out.println("weekday");
            case 6, 7 -> System.out.println("weekend");
            default -> System.out.println("invalid");
        }

        String type = switch (day) {
            case 6, 7 -> "weekend";
            default -> "weekday";
        };
        System.out.println(type);
    }
}
Output
weekend
weekend

Friends & Partners