Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the new keyword "yield" mean in Java 13?

Java 13 introduced the yield keyword for switch expressions.

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

like image 733
zerocewl Avatar asked Sep 22 '19 12:09

zerocewl


People also ask

Does Java have yield?

Several programming languages, such as Ruby or Python to name a few, provides the yield command. Yield provides an effective way, in terms of memory consumption, to create series of values, by generating such values on demand. More information on Python Yield.

What is switch expression in Java?

Java SE 12 introduced switch expressions, which (like all expressions) evaluate to a single value, and can be used in statements. It also introduced "arrow case " labels that eliminate the need for break statements to prevent fall through.

What is enhanced switch in Java?

In Java 13 enhanced switch is a preview feature, which needs to be explicitly enabled. You can now use case for multiple values. In addition to the traditional switch statement, you can use switch expression, which returns a value. To return a value from a switch expression you can use: break in Java 12.

How do you use expressions in switch case in Java?

The switch case in Java works like an if-else ladder, i.e., multiple conditions can be checked at once. Switch is provided with an expression that can be a constant or literal expression that can be evaluated. The value of the expression is matched with each test case till a match is found.


1 Answers

Q&A

How can I use it?

  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;     } }; 
  2. 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?

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?

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

Specification

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.

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.

like image 128
Andrew Tobilko Avatar answered Oct 10 '22 07:10

Andrew Tobilko