switch表达式
使用switch时,如果遗漏了break,就会造成严重的逻辑错误,而且不易在源代码中发现错误。从Java 12开始,switch语句升级为更简洁的表达式语法,使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要break语句:
public class Main {public static void main(String[] args) {String fruit = "apple";switch (fruit) {case "apple" -> System.out.println("Selected apple");case "pear" -> System.out.println("Selected pear");case "mango" -> {System.out.println("Selected mango");System.out.println("Good choice!");}default -> System.out.println("No fruit selected");}}
}
注意新语法使用->,如果有多条语句,需要用{}括起来。不要写break语句,因为新语法只会执行匹配的语句,没有穿透效应。
很多时候,我们还可能用switch语句给某个变量赋值。例如:
int opt;
switch (fruit) {
case "apple":opt = 1;break;
case "pear":
case "mango":opt = 2;break;
default:opt = 0;break;
}
使用新的switch语法,不但不需要break,还可以直接返回值。把上面的代码改写如下:
public class Main {public static void main(String[] args) {String fruit = "apple";int opt = switch (fruit) {case "apple" -> 1;case "pear", "mango" -> 2;default -> 0;}; // 注意赋值语句要以;结束System.out.println("opt = " + opt);}
}
这样可以获得更简洁的代码。
yield
大多数时候,在switch表达式内部,我们会返回简单的值。
但是,如果需要复杂的语句,我们也可以写很多语句,放到{…}里,然后,用yield返回一个值作为switch语句的返回值:
public class Main {public static void main(String[] args) {String fruit = "orange";int opt = switch (fruit) {case "apple" -> 1;case "pear", "mango" -> 2;default -> {int code = fruit.hashCode();yield code; // switch语句返回值}};System.out.println("opt = " + opt);}
}