新关键字"yield"是什么意思在Java 13中意味着什么? [英] What does the new keyword "yield" mean in Java 13?

查看:395
本文介绍了新关键字"yield"是什么意思在Java 13中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 13为switch表达式引入了 yield 关键字.

Java 13 introduced the yield keyword for switch expressions.

我该如何使用它?它与默认的returnbreak值有什么区别?

How can I use it and what's the difference to a default return or break value?

推荐答案

Q& A

我如何使用它?

How can I use it?

  1. 当需要整块时,带有箭头标签:

  1. With arrow labels when a full block is needed:

int value = switch (greeting) {
    case "hi" -> {
        System.out.println("I am not just yielding!");
        yield 1;
    }
    case "hello" -> {
        System.out.println("Me too.");
        yield 2;
    }
    default -> {
        System.out.println("OK");
        yield -1;
    }
};

  • 使用传统积木:

  • With traditional blocks:

    int value = switch (greeting) {
        case "hi":
            System.out.println("I am not just yielding!");
            yield 1;
        case "hello":
            System.out.println("Me too.");
            yield 2;
        default:
            System.out.println("OK");
            yield -1;
    };
    

  • 默认收益有什么区别?

    What's the difference to a default return?

    return语句将控制权返回给方法的调用者(

    A return statement returns control to the invoker of a method (§8.4, §15.12) or constructor (§8.8, §15.9) while a yield statement transfers control by causing an enclosing switch expression to produce a specified value.

    中断值有什么区别?

    What's the difference to a break value?

    删除具有值的break语句,而使用yield语句.

    The break with value statement is dropped in favour of a yield statement.

    JEP 354规范附在 JLS 13 上,它总结了我们需要的一切了解新的switch.请注意,它尚未合并到语言规范中,因为它仍然是预览功能,因此,还不是该语言的永久组成部分.

    There is Specification for JEP 354 attached to the JLS 13 which sums up everything we need to know about the new switch. Note that it wasn't merged into the language specification because it's still a preview feature and, thus, not yet a permanent part of the language.

    yield语句通过使封闭的switch表达式产生指定值来转移控制权.

    A yield statement transfers control by causing an enclosing switch expression to produce a specified value.

    YieldStatement:
        yield Expression;
    

    yield语句尝试将控制权转移到最内层的封闭开关表达式.该表达式称为产量目标,然后立即正常完成,并且Expression的值变为switch表达式的值.

    A yield statement attempts to transfer control to the innermost enclosing switch expression; this expression, which is called the yield target, then immediately completes normally and the value of the Expression becomes the value of the switch expression.

    • 如果yield语句没有屈服目标,则是编译时错误.

    • It is a compile-time error if a yield statement has no yield target.

    如果yield目标包含包围yield语句的任何方法,构造函数,初始化程序或lambda表达式,则是编译时错误.

    It is a compile-time error if the yield target contains any method, constructor, initializer, or lambda expression that encloses the yield statement.

    如果yield语句的Expression为空(15.1),则是编译时错误.

    It is a compile-time error if the Expression of a yield statement is void (15.1).

    执行yield语句首先对Expression求值.如果Expression的评估由于某种原因而突然完成,则yield语句由于该原因而突然完成.如果Expression的评估正常完成,产生一个值V,则yield语句将突然完成,原因是产生的值为V的产量.

    Execution of a yield statement first evaluates the Expression. If the evaluation of the Expression completes abruptly for some reason, then the yield statement completes abruptly for that reason. If evaluation of the Expression completes normally, producing a value V, then the yield statement completes abruptly, the reason being a yield with value V.

    这篇关于新关键字"yield"是什么意思在Java 13中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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