什么是switch表达式,它们与switch语句有什么不同?
作为 Java SE 12 的一部分,引入了switch表达式,并且从 Java SE 14 开始,它们已经标准化。它们与switch陈述有何不同?
回答
该switch语句:
与if/else if/else语句不同,switch语句可以有多个可能的执行路径。阿switch与原始类型的作品,byte,short,char,和int,它们各自包装类型(Byte,Short,Character,和Integer),枚举类型,和String类型1。而一个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.
The order is partially paid. Some features will not be available. Please check the brochure for details.
The order is partially paid. Some features will not be available. Please check the brochure for details.
该switch表达式:
该switch表达式是在 Java SE 12 中引入的。但是,它在 Java SE 12 和 13 中保留为预览功能,并最终在 Java SE 14 中标准化。与任何表达式一样,switch表达式计算为单个值,并且可以在语句中使用。它还引入了“箭头case”标签,消除了break防止跌落的声明的需要。从 Java SE 15 开始,支持的数据类型没有变化(在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);
}
}
输出:
在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);
}
}
输出:
1对StringJDK 7的支持
回答
写得不错!但我可能还会为单个 case 语句添加多个 case 的功能。下面的例子非常做作(有很多更好的方法来实现它)。它对字符串中的元音、数字、辅音和其他字符进行简单的频率计数。
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