Java 13 –开关表达式

切换表达

在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

参考文献

翻译自: https://mkyong.com/java/java-13-switch-expressions/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值