switch
语句或表达式的 switch
语句。
示例:
double getPrice(String fruit) {
// Switch 语句可以替换为增强型 'switch'
switch (fruit) {
case "Apple":
return 1.0;
case "Orange":
return 1.5;
case "Mango":
return 2.0;
default:
throw new IllegalArgumentException();
}
}
在应用快速修复后:
double getPrice(String fruit) {
return switch (fruit) {
case "Apple" -> 1.0;
case "Orange" -> 1.5;
case "Mango" -> 2.0;
default -> throw new IllegalArgumentException();
};
}
仅当项目或模块的语言级别为 14 或更高时,此检查才会报告。
2019.1 最新变化