> ## 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.

# Java Yield Statement

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

```java theme={"dark"}
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.

```java theme={"dark"}
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.

```java theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
