1 回答

TA貢獻1829條經驗 獲得超7個贊
yield在 Java 13 中使用
在 Java 13 中,switch 表達式使用新的受限標識符1yield從塊中返回值:
return Optional.ofNullable(
switch (methodStr.strip().toUpperCase(Locale.ROOT)) {
case "GET" -> RequestMethod.GET;
// ... rest omitted
default -> {
log.warn("Unsupported request method: '{}'", methodStr);
// yield instead of return
yield null;
}
});
break在 Java 12 中使用
在 Java 12 中,switch 表達式用于break從塊中返回值:
return Optional.ofNullable(
switch (methodStr.strip().toUpperCase(Locale.ROOT)) {
case "GET" -> RequestMethod.GET;
// ... rest omitted
default -> {
log.warn("Unsupported request method: '{}'", methodStr);
// break instead of return
break null;
}
});
1yield
不是關鍵字,正如用戶 skomisa 所指出的那樣。
添加回答
舉報