int num = 1;
switch (num) {
case 1: System.out.println("1");
case 2: System.out.println("2"); break;
case 3: System.out.println("3"); break;
default: System.out.println("4");
}
1
2
switch 匹配到 case 1 後,**順序執行**後續所有語句,直到遇到 break 或 switch 結束。case 的常量值是否匹配,**匹配只是入口**,進入後按順序落下。case 1: case 2: case 3: doSomething(); break;case 邏輯結束**必須加 break**(或 return/throw)。switch 表達式(-> 箭頭語法),**無穿透**,更安全:String result = switch (num) {
case 1 -> "一";
case 2 -> "二";
default -> "其他";
};// fall through 供編譯器/靜態分析工具識別。