在Java 13中, JEP 354:交换表达式通过添加新的yield
关键字从交换表达式返回值来扩展了先前的Java 12交换表达式。
PS Switch表达式是预览功能,默认情况下处于禁用状态。
注意
这是Java 14中的标准功能。
1.没有更多的价值突破!
1.1 Java 12 value breaks
语法不再在Java 13中编译,而是使用yield
。
// value breaks are superseded by Java 13 'yield' statements.
private static int getValueViaBreak(String mode) {
int result = switch (mode) {
case "a":
case "b":
break 1;
case "c":
break 2;
case "d":
case "e":
case "f":
break 3;
default:
break -1;
};
return result;
}
1.2在Java 13中,我们可以使用yield
返回一个值。
private static int getValueViaYield(String mode) {
int result = switch (mode) {
case "a", "b":
yield 1;
case "c":
yield 2;
case "d", "e", "f":
// do something here...
System.out.println("Supports multi line block!");
yield 3;
default:
yield -1;
};
return result;
}
1.3 Java 13仍支持switch
表达式(Java 12)的标签规则或箭头。
private static int getValueViaArrow(String mode) {
int result = switch (mode) {
case "a", "b" -> 1;
case "c" -> 2;
case "d", "e", "f" -> {
// do something here...
System.out.println("Supports multi line block!");
yield 3;
}
default -> -1;
};
return result;
}
2. Java 13开关表达式
Java 13中的完整switch
表达式示例。
JEP354.java
package com.mkyong.java13;
public class JEP354 {
public static void main(String[] args) {
System.out.println(getValueViaYield("a"));
System.out.println(getValueViaYield("c"));
System.out.println(getValueViaYield("e"));
System.out.println(getValueViaYield("z"));
}
// Traditional switch
private static int getValueBefore12(String mode) {
int result;
switch (mode) {
case "a":
case "b":
result = 1;
break;
case "c":
result = 2;
break;
case "d":
case "e":
case "f":
result = 3;
break;
default:
result = -1;
}
;
return result;
}
// Java 12, multiple comma-separated labels
private static int getValueMultipleLabels(String mode) {
int result;
switch (mode) {
case "a", "b":
result = 1;
break;
case "c":
result = 2;
break;
case "d", "e", "f":
result = 3;
break;
default:
result = -1;
}
;
return result;
}
// Java 13, value breaks are superseded by 'yield' statements
// Java 12, switch expression returning value via break
/*private static int getValueViaBreak(String mode) {
int result = switch (mode) {
case "a":
case "b":
break 1;
case "c":
break 2;
case "d":
case "e":
case "f":
break 3;
default:
break -1;
};
return result;
}*/
// Java 12, switch expression returns a value via label rules (arrow)
private static int getValueViaArrow(String mode) {
int result = switch (mode) {
case "a", "b" -> 1;
case "c" -> 2;
case "d", "e", "f" -> {
// do something here...
System.out.println("Supports multi line block!");
yield 3;
}
default -> -1;
};
return result;
}
// Java 13, switch expression returns a value via yield
private static int getValueViaYield(String mode) {
int result = switch (mode) {
case "a", "b":
yield 1;
case "c":
yield 2;
case "d", "e", "f":
// do something here...
System.out.println("Supports multi line block!");
yield 3;
default:
yield -1;
};
return result;
}
}
输出量
1
2
Supports multi line block!
3
-1
3.启用预览功能
3.1正常的javac
编译将提示以下错误:
$ javac JEP354.java
D:\test>javac JEP354.java
JEP354.java:39: error: multiple case labels are a preview feature and are disabled by default.
case "a", "b":
^
(use --enable-preview to enable multiple case labels)
JEP354.java:76: error: switch expressions are a preview feature and are disabled by default.
int result = switch (mode) {
^
(use --enable-preview to enable switch expressions)
JEP354.java:77: error: switch rules are a preview feature and are disabled by default.
case "a", "b" -> 1;
^
(use --enable-preview to enable switch rules)
3 errors
3.2我们需要手动启用预览功能:
javac --enable-preview --release 13 Example.java
java --enable-preview Example