什么是 switch 表达式,它们与 switch 语句有何不同?
2022-09-02 11:51:30
作为Java SE 12的一部分,引入了开关
表达式,并且自Java SE 14以来,它们已经标准化。它们与语句有何不同?switch
作为Java SE 12的一部分,引入了开关
表达式,并且自Java SE 14以来,它们已经标准化。它们与语句有何不同?switch
switch
与该语句不同,一个语句可以有多个可能的执行路径。A 适用于基元类型、、、 和 、它们各自的包装类型 (、 、 和 )、枚举类型和类型1。语句用于基于值或条件的范围测试表达式,而语句用于仅基于单个值测试表达式。if/else if/else
switch
switch
byte
short
char
int
Byte
Short
Character
Integer
String
if-else
switch
演示
enum PaymentStatus {
UNPAID, PARTPAID, PAID, DISPUTED, UNKNOWN;
}
public class Main {
public static void main(String[] args) {
String message = "";
PaymentStatus paymentStatus = PaymentStatus.PARTPAID;
switch (paymentStatus) {
case UNPAID:
message = "The order has not been paid yet. Please make the minimum/full amount to procced.";
break;
case PARTPAID:
message = "The order is partially paid. Some features will not be available. Please check the brochure for details.";
break;
case PAID:
message = "The order is fully paid. Please choose the desired items from the menu.";
break;
default:
throw new IllegalStateException("Invalid payment status: " + paymentStatus);
}
System.out.println(message);
}
}
输出:
The order is partially paid. Some features will not be available. Please check the brochure for details.
switch
该表达式是在 Java SE 12 中引入的。但是,它仍然是Java SE 12和13中的预览功能,并最终与Java SE 14一起标准化。与任何表达式一样,表达式的计算结果为单个值,并且可以在语句中使用。它还引入了“箭头”标签,消除了对语句的需要以防止掉落。从 Java SE 15 开始,支持的数据类型没有变化(在上面的语句部分提到)。switch
switch
case
break
switch
演示
enum PaymentStatus {
UNPAID, PARTPAID, PAID, DISPUTED, UNKNOWN;
}
public class Main {
public static void main(String[] args) {
PaymentStatus paymentStatus = PaymentStatus.PARTPAID;
String message = switch (paymentStatus) {
case UNPAID -> "The order has not been paid yet. Please make the minimum/full amount to procced.";
case PARTPAID -> "The order is partially paid. Some features will not be available. Please check the brochure for details.";
case PAID -> "The order is fully paid. Please choose the desired items from the menu.";
default -> throw new IllegalStateException("Invalid payment status: " + paymentStatus);
};
System.out.println(message);
}
}
输出:
The order is partially paid. Some features will not be available. Please check the brochure for details.
switch
yield
从 Java SE 13 开始,您可以使用语句而不是箭头运算符 (->) 从表达式返回值。yield
switch
演示
enum PaymentStatus {
UNPAID, PARTPAID, PAID, DISPUTED, UNKNOWN;
}
public class Main {
public static void main(String[] args) {
PaymentStatus paymentStatus = PaymentStatus.PARTPAID;
String message = switch (paymentStatus) {
case UNPAID:
yield "The order has not been paid yet. Please make the minimum/full amount to procced.";
case PARTPAID:
yield "The order is partially paid. Some features will not be available. Please check the brochure for details.";
case PAID:
yield "The order is fully paid. Please choose the desired items from the menu.";
default:
throw new IllegalStateException("Invalid payment status: " + paymentStatus);
};
System.out.println(message);
}
}
输出:
The order is partially paid. Some features will not be available. Please check the brochure for details.
1 在 JDK 7 中添加了 对 的支持String
写得不错!但是,我还可以添加为单个案例语句设置多个案例的功能。下面的例子是非常人为的(有很多更好的方法可以实现它)。它对字符串中的元音、数字、辅音和其他字符进行简单的频率计数。
int count[] = new int[4];
String s = "829s2bi9jskj*&@)(so2i2ksso";
for (char c : s.toCharArray()) {
int i = switch (c) {
case 'a', 'e', 'i', 'o', 'u' -> 0;
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> 1;
case 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w',
'x', 'y', 'z' -> 2;
default -> 3;
};
count[i]++;
}
System.out.printf("vowels - %d%n", count[0]);
System.out.printf("digits - %d%n", count[1]);
System.out.printf("consonants - %d%n", count[2]);
System.out.printf("other - %d%n", count[3]);
指纹
vowels - 4
digits - 7
consonants - 10
other - 5