Java 13 中的新关键字 "yield" 是什么意思?
What does the new keyword "yield" mean in Java 13?
Java 13 为 switch
表达式引入了 yield
关键字。
我该如何使用它,它与默认 return
或 break
值有何区别?
作为 JEP 354 (Java 13) 的一部分,您可以 yield 开关中的值(可选择将其分配给变量)
yield statement to yield a value, which becomes the value of the enclosing switch expression.
int j = switch (day) {
case MONDAY -> 0;
case TUESDAY -> 1;
default -> {
int k = day.toString().length();
int result = f(k);
yield result;
}
};
我认为您对 Java 12 上的 JEP 325 使用 break 到 return 值感到困惑:
we have extended the break statement to take an argument, which becomes the value of the enclosing switch expression.
int j = switch (day) {
case MONDAY -> 0;
case TUESDAY -> 1;
default -> {
int k = day.toString().length();
int result = f(k);
break result;
此外,您甚至可以使用lambda syntax
boolean result = switch (ternaryBool) {
case TRUE -> true;
case FALSE -> false;
case FILE_NOT_FOUND -> throw new UncheckedIOException(
"This is ridiculous!",
new FileNotFoundException());
// as we'll see in "Exhaustiveness", `default` is not necessary
default -> throw new IllegalArgumentException("Seriously?! ");
};
With switch expressions, the entire switch block "gets a value" that can then be assigned; you can use a lambda-style syntax
While Java 12 introduces and 13 refines switch expressions, they do so as a preview language feature. That means (a) it can still change over the next few releases (as it did between 12 and 13) and (b) it needs to be unlocked, at compile time and run time, with the new command line option --enable-preview. Then keep in mind that this isn’t the endgame for switch – it’s just a step on the way to full pattern matching.
yield
标记要从 switch 分支返回的值。它终止了 switch 表达式,你不需要在它后面有 break。
来自doc
The two statements, break (with or without a label) and yield,
facilitate easy disambiguation between switch statements and switch
expressions: a switch statement but not a switch expression can be the
target of a break statement; and a switch expression but not a switch
statement can be the target of a yield statement.
它还提供,NullPointerException
安全,
String message = switch (errorCode) {
case 404:
yield "Not found!";
case 500:
yield "Internal server error!";
// No default
};
这将导致,
the switch expression does not cover all possible input values
问答
How can I use it?
需要完整块时使用箭头标签:
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;
}
};
使用传统积木:
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
语句 returns 控制到 方法的调用者 (§8.4, §15.12) or constructor (§8.8, §15.9) 而 yield
语句转移通过 使封闭的 switch
表达式 产生指定值进行控制。
What's the difference to a break value?
break
with value 语句被删除,取而代之的是 yield
语句。
规格
有 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,因此,还不是语言的永久部分。
A yield
statement transfers control by causing an enclosing switch
expression to produce a specified value.
YieldStatement:
yield Expression;
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.
It is a compile-time error if a yield
statement has no yield target.
It is a compile-time error if the yield
target contains any method, constructor, initializer, or lambda expression that encloses the yield statement.
It is a compile-time error if the Expression
of a yield
statement is void (15.1).
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
.
break replace with yield in java 13. 这是java 13.in Java 12中定义的预览功能之一,我们可以使用break来return 来自开关的值。但是在 java 13 yield use for the return value from switch expression.
In Java 13 break replace by yield,
String number = switch (number) {
case 1:
yield "one";
case 2:
yield "two";
default:
yield "Zero";
}
Java13.
中仍然支持箭头语法
String number = switch (number) {
case 1 -> "one";
case 2 -> "two";
default -> "Zero";
}
Java 13 为 switch
表达式引入了 yield
关键字。
我该如何使用它,它与默认 return
或 break
值有何区别?
作为 JEP 354 (Java 13) 的一部分,您可以 yield 开关中的值(可选择将其分配给变量)
yield statement to yield a value, which becomes the value of the enclosing switch expression.
int j = switch (day) { case MONDAY -> 0; case TUESDAY -> 1; default -> { int k = day.toString().length(); int result = f(k); yield result; } };
我认为您对 Java 12 上的 JEP 325 使用 break 到 return 值感到困惑:
we have extended the break statement to take an argument, which becomes the value of the enclosing switch expression.
int j = switch (day) { case MONDAY -> 0; case TUESDAY -> 1; default -> { int k = day.toString().length(); int result = f(k); break result;
此外,您甚至可以使用lambda syntax
boolean result = switch (ternaryBool) { case TRUE -> true; case FALSE -> false; case FILE_NOT_FOUND -> throw new UncheckedIOException( "This is ridiculous!", new FileNotFoundException()); // as we'll see in "Exhaustiveness", `default` is not necessary default -> throw new IllegalArgumentException("Seriously?! "); };
With switch expressions, the entire switch block "gets a value" that can then be assigned; you can use a lambda-style syntax
While Java 12 introduces and 13 refines switch expressions, they do so as a preview language feature. That means (a) it can still change over the next few releases (as it did between 12 and 13) and (b) it needs to be unlocked, at compile time and run time, with the new command line option --enable-preview. Then keep in mind that this isn’t the endgame for switch – it’s just a step on the way to full pattern matching.
yield
标记要从 switch 分支返回的值。它终止了 switch 表达式,你不需要在它后面有 break。
来自doc
The two statements, break (with or without a label) and yield, facilitate easy disambiguation between switch statements and switch expressions: a switch statement but not a switch expression can be the target of a break statement; and a switch expression but not a switch statement can be the target of a yield statement.
它还提供,NullPointerException
安全,
String message = switch (errorCode) {
case 404:
yield "Not found!";
case 500:
yield "Internal server error!";
// No default
};
这将导致,
the switch expression does not cover all possible input values
问答
How can I use it?
需要完整块时使用箭头标签:
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; } };
使用传统积木:
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
语句 returns 控制到 方法的调用者 (§8.4, §15.12) or constructor (§8.8, §15.9) 而 yield
语句转移通过 使封闭的 switch
表达式 产生指定值进行控制。
What's the difference to a break value?
break
with value 语句被删除,取而代之的是 yield
语句。
规格
有 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,因此,还不是语言的永久部分。
A
yield
statement transfers control by causing an enclosingswitch
expression to produce a specified value.YieldStatement: yield Expression;
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 theExpression
becomes the value of theswitch
expression.
It is a compile-time error if a
yield
statement has no yield target.It is a compile-time error if the
yield
target contains any method, constructor, initializer, or lambda expression that encloses the yield statement.It is a compile-time error if the
Expression
of ayield
statement is void (15.1).Execution of a
yield
statement first evaluates theExpression
. If the evaluation of theExpression
completes abruptly for some reason, then theyield
statement completes abruptly for that reason. If evaluation of theExpression
completes normally, producing a valueV
, then theyield
statement completes abruptly, the reason being a yield with valueV
.
break replace with yield in java 13. 这是java 13.in Java 12中定义的预览功能之一,我们可以使用break来return 来自开关的值。但是在 java 13 yield use for the return value from switch expression.
In Java 13 break replace by yield,
String number = switch (number) {
case 1:
yield "one";
case 2:
yield "two";
default:
yield "Zero";
}
Java13.
中仍然支持箭头语法String number = switch (number) {
case 1 -> "one";
case 2 -> "two";
default -> "Zero";
}