Object obj = ...;
String result = switch (obj) {
case Integer i -> "int: " + i;
case String s -> "str: " + s;
default -> "other"
};
可直接返回值,无需 break,支持 yield。
int day = switch (d) {
case MONDAY, FRIDAY, SUNDAY -> 6;
default -> {
int x = getBase();
yield x + 1;
}
};