什么是 switch 表达式,它们与 switch 语句有何不同? [英] What are switch expressions and how are they different from switch statements?

查看:18
本文介绍了什么是 switch 表达式,它们与 switch 语句有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As part of Java SE 12, switch expressions were introduced and since Java SE 14, they have been standardized. How are they different from switch statements?

推荐答案

switch语句:

if/else if/else 语句不同,switch 语句可以有许多可能的执行路径.switch 适用于原始类型 byteshortcharint、它们各自的包装类型(ByteShortCharacterInteger)、枚举类型和字符串类型1.if-else 语句用于测试基于值或条件范围的表达式,而 switch 语句用于测试仅基于单个值的表达式.

The switch statement:

Unlike the if/else if/else statement, a switch statement can have a number of possible execution paths. A switch works with the primitive types, byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type1. While an if-else statement is used to test expressions based on ranges of values or conditions, a switch statement is used to test expressions based only on a single value.

演示

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 中仍作为 Preview 功能,最终在 Java SE 14 中实现了标准化.像任何表达式switch表达式评估为单个值,并且可以在语句中使用.它还引入了箭头case".标签消除了对 break 语句的需要以防止失败.从 Java SE 15 开始,支持的数据类型没有变化(在上面的 switch 语句部分中提到).

The switch expression:

The switch expression was introduced with Java SE 12. However, it remained as a Preview feature in Java SE 12 and 13 and finally got standardized with Java SE 14. Like any expression, switch expressions evaluate to a single value, and can be used in statements. It also introduced "arrow case" labels eliminating the need for break statements to prevent fall through. As of Java SE 15, there is no change in the supported data types (mentioned in the switch statement section above).

演示

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.

带有 yieldswitch 表达式:

从 Java SE 13 开始,您可以使用 yield 语句代替箭头运算符 (->) 从 switch 表达式返回值.

The switch expression with yield:

Since Java SE 13, you can use yield statement, instead of the arrow operator (->), to return a value from a switch expression.

演示

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 的支持


1 The support for String was added with JDK 7

这篇关于什么是 switch 表达式,它们与 switch 语句有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆