Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The yield statement in Java is a context-sensitive keyword introduced in Java 14 (JEP 361) used to explicitly return a value from a block within a switch expression. It transfers control and the evaluated result of its target expression back to the enclosing switch expression, acting analogously to a return statement but scoped strictly to the switch block rather than the enclosing method.

Syntax

yield <expression>;

Mechanics and Execution Rules

  • Switch Expressions Only: yield can only be utilized within a switch expression (a switch construct that evaluates to a single value). It results in a compilation error if used within a traditional switch statement.
  • Block Scoping: It is mandatory when a branch of a switch expression requires a full code block { ... } to execute multiple statements before producing a value.
  • Type Compatibility: The <expression> provided to the yield statement must be assignment-compatible with the declared or inferred type of the enclosing switch expression.
  • Exhaustiveness: Because yield is tied to switch expressions, the compiler enforces exhaustiveness. Every possible execution path within the switch must either yield a value or throw an exception.
  • Restricted Identifier: To maintain backward compatibility, yield is a restricted identifier, not a globally reserved keyword. It acts as a keyword only within the context of a switch block. It remains valid to use yield as a variable or method name elsewhere in the codebase.

Syntax Visualization

Arrow Syntax (->) with Blocks When using the arrow syntax, single expressions implicitly yield their value. The yield keyword is only required when opening a block.
int result = switch (targetVariable) {
    case "A" -> 1; // Implicit yield
    case "B" -> {
        System.out.println("Processing B");
        yield 2;   // Explicit yield required inside a block
    }
    default -> throw new IllegalArgumentException();
};
Traditional Colon Syntax (:) in Expressions If a switch expression utilizes traditional colon syntax (fall-through semantics), yield replaces the break statement to simultaneously prevent fall-through and provide the branch’s evaluated value.
int result = switch (targetVariable) {
    case "A":
    case "B":
        yield 1; // Provides value and exits the switch expression
    case "C":
        System.out.println("Processing C");
        yield 2;
    default:
        yield 0;
};

yield vs. return

It is critical to distinguish yield from return regarding control flow:
  • yield terminates the execution of the switch expression and provides a value to the variable capturing the expression’s result.
  • return terminates the execution of the enclosing method or lambda, passing control back to the caller. Using return inside a switch expression is a compilation error.
Master Java with Deep Grasping Methodology!Learn More