Java12 新特性
升级的switch语句
在jdk12之前的switch语句中,如果没有写break,则会出现case穿透现象
int month = 3;
switch (month) {
case 3:
case 4:
case 5:
System.out.println("spring");
break;
case 6:
case 7:
case 8:
System.out.println("summer");
break;
case 9:
case 10:
case 11:
System.out.println("autumn");
break;
case 12:
case 1:
case 2:
System.out.println("winter");
break;
default:
System.out.println("wrong");
break;
}
在jdk12之后可以省略全部的break和部分case,这样使用
int month = 3;
switch (month) {
case 3,4,5 -> System.out.println("spring");
case 6,7,8 -> System.out.println("summer");
case 9,10,11 -> System.out.println("autumn");
case 12, 1,2 -> System.out.println("winter");
default -> System.out.println("wrong");
}