Java8 引入字符串和枚举
Java21 可以返回值, yield关键字, switch 表达式, 模式匹配, null值处理
public static void java8() {
String day = "tuesday";
switch (day) {
case "monday":
System.out.println("work");
break;
case "tuesday":
System.out.println("work");
break;
case "saturday":
System.out.println("day off");
break;
case "sunday":
System.out.println("day off");
break;
}
//枚举值monday,tuesday,wednesday,thursday,friday,saturday,sunday;
Days days = Days.saturday;
switch (days) {
case Days.monday:
System.out.println("work");
break;
case Days.tuesday:
System.out.println("work");
break;
case Days.saturday:
System.out.println("day off");
break;
case Days.sunday:
System.out.println("day off");
break;
}
}
运行结果
public static String java21() {
//null 处理
var name = switch (getName()) {
case "" -> "";
case "1" -> "num";
case null -> "null";
default -> "unknown";
};
System.out.println(name);
//模式匹配
Object o = 1;
Object a = switch (o) {
case Integer i -> 1 + i;
case String s -> "1" + s;
default -> o.toString();
};
System.out.println(a);
//yield , 表达式
String day = "sunday";
return switch (day) {
case "monday", "tuesday" -> "work";
case "saturday", "sunday" -> {
System.out.println("day off");
yield "work";
}
default -> "unknown";
};
}
static String getName() {
return null;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/1670ab90491845e7837694481e89eab7.png