Language syntax
Java yield statement
Provide the result from a block within a switch expression. Transfer control to that expression, not to the method caller.
Formal Syntax & Structure
java syntaxjava
yield expression;Working Example
Example Snippetjava
public class Main { public static void main(String[] args) { int code = 2; int result = switch (code) { case 2 -> { int base = 6; yield base * 2; } default -> 0; }; System.out.println(result); }}