新关键字"yield"是什么意思在Java 13中意味着什么? [英] What does the new keyword "yield" mean in Java 13?
问题描述
Java 13为switch
表达式引入了 yield
关键字.
Java 13 introduced the yield
keyword for switch
expressions.
我该如何使用它?它与默认的return
或break
值有什么区别?
How can I use it and what's the difference to a default return
or break
value?
推荐答案
Q& A
我如何使用它?
How can I use it?
-
当需要整块时,带有箭头标签:
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
语句将控制权返回给方法的调用者(§15.12)或构造函数(§15.9),而yield
语句通过使switch
括起来到产生指定的值.
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 enclosingswitch
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屋!