Java switch case
switch 语法、case 穿透、字符串匹配与 Java 14 箭头语法。
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.
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);
}
}Well done
Your grade is Ccase 穿透
Without break, execution falls through into the next case. Sometimes that is intentional (several values sharing logic), but usually it is a bug.
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();
}
}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.
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");
}
}
}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.
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);
}
}weekend
weekendswitch 语法
switch 按表达式的值跳转到匹配的 case,直到遇到 break 或结束。表达式支持 int、char、String、枚举等,不支持 long 和浮点。
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);
}
}Well done
Your grade is Ccase 穿透
某个 case 结尾没有 break,就会继续执行下一个 case 的语句,这叫「穿透」。它有时是有意利用(多个值共享逻辑),多数时候是 bug。
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();
}
}weekday
two three 第二个 switch 的 day 是 2,因此从 case 2 开始匹配,case 1 的 one 不会被打印;之后一路穿透到 case 3,所以只有 two 和 three。把 case 1 改成 case 2 才会输出 one two three。
字符串 switch
Java 7 起 switch 支持 String,底层用 hashCode 加 equals 比较。注意表达式为 null 时会抛 NullPointerException,要先判空。
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");
}
}
}starting...Java 14 箭头语法
case X -> 语句 不会穿透,也不需要 break;多语句用大括号包起来。整个 switch 还能作为表达式赋值给变量,yield 用于在大括号里返回值。
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);
}
}weekend
weekend