gpt4 book ai didi

java - 什么是switch表达式,它们与switch语句有何不同?

转载 作者:行者123 更新时间:2023-12-03 10:03:32 25 4
gpt4 key购买 nike

作为Java SE 12的一部分,引入了 switch expressions,自Java SE 14起,它们已被标准化。它们与switch语句有何不同?

最佳答案

switch语句:
if/else if/else语句不同,switch语句可以具有许多可能的执行路径。 switch使用原始类型byteshortcharint,它们各自的包装器类型(ByteShortCharacterInteger),枚举类型和String type1起作用。虽然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表达式: switch表达式是在Java SE 12中引入的。但是,它保留为Java SE 12和13中的 预览功能,并最终通过Java SE 14进行了标准化。陈述。它还引入了“arrow switch”标签,从而消除了对 case语句的预防需求。从Java SE 15开始,支持的数据类型没有变化(在上面的 break语句部分中提到)。
演示
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.
带有 switchswitch表达式:
从Java SE 13开始,您可以使用 yield语句而不是箭头运算符(->)从 yield表达式返回值。
演示
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中添加了对 switch的支持

关于java - 什么是switch表达式,它们与switch语句有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65657169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com