Skip to content

Commit 9a15181

Browse files
committed
docs(Switch): add explanation of yield in Java 12+ switch expressions
What - Documented the role and usage of the yield keyword introduced with switch expressions in Java 12+. - Covered: 1. Definition: yield returns a value from a case block in switch expressions. 2. When to use: required for multi-line case blocks where arrow syntax (->) isn’t sufficient. 3. Examples: - Arrow-style cases without yield. - Block-style cases with yield returning values. 4. Why not return: return exits the whole method; yield only exits the case block. 5. Comparison table of break, return, and yield with scope and behavior. 6. Summary: yield is the modern replacement for break when using switch expressions that return values. Why - Clarifies difference between traditional switch statements and new switch expressions. - Explains why yield was introduced and how it fits into Java’s expression-oriented switch. - Provides examples for practical usage and avoids confusion with return or break. How - Illustrated simple switch expressions using arrow syntax (no yield needed). - Provided block-style examples where yield is necessary to return values. - Added table comparing break, return, and yield in terms of purpose, value return, and scope. Logic - Inputs: integer values tested in switch expression. - Outputs: returned string values based on case. - Flow: 1. If single expression, arrow syntax works without yield. 2. If multiple statements, case must use yield to return a value. 3. Default branch also uses yield to return fallback value. - Edge cases: - Using return in case block exits method prematurely, not just switch. - Complexity / performance: identical to traditional switch; yield only changes syntax and semantics. - Concurrency / thread-safety: not relevant; purely syntactic feature. - Error handling: ensures compile-time safety by requiring yield in multi-statement blocks. Real-life applications - Replacing verbose if-else chains with concise switch expressions. - Cleaner code for mapping values or computing results based on enums, constants, or input values. - Safer than traditional switch with fall-through, reducing bugs. Notes - yield is mandatory for block-style cases in switch expressions. - Arrow-style cases remain concise and preferred when logic fits in one expression. - Break is still used in old-style switch statements, but not needed in modern switch expressions. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a866547 commit 9a15181

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Understanding `yield` in Java 12+ Switch Expressions:
2+
3+
1. What is `yield`?
4+
- In traditional `switch` statements:
5+
- You used `break` to exit a case.
6+
- No value was returned directly.
7+
8+
- In Java 12+, switch expressions allow cases to **return values**.
9+
- `yield` is the keyword that returns a value from a `case` block (especially multi-line blocks with `{}`).
10+
11+
12+
2. When to Use `yield`
13+
- When you have multi-line logic in a case.
14+
- When you need to return a computed value back to the switch expression.
15+
- When arrow syntax (`->`) is not enough.
16+
17+
3. Examples
18+
19+
Arrow style (no `yield` needed):
20+
String result = switch (value) {
21+
case 1 -> "One";
22+
case 2 -> "Two";
23+
default -> "Other";
24+
};
25+
26+
Block style (must use `yield`):
27+
String result = switch (value) {
28+
case 1 -> {
29+
System.out.println("Matched one");
30+
yield "One"; // returns "One" from this case block
31+
}
32+
case 2 -> {
33+
System.out.println("Matched two");
34+
yield "Two"; // returns "Two"
35+
}
36+
default -> {
37+
yield "Other";
38+
}
39+
};
40+
41+
42+
4. Why Not `return`?
43+
- `return` exits the whole method.
44+
- `yield` only exits the current `case` block and provides a value back to the `switch`.
45+
46+
47+
5. Quick Comparison
48+
| Keyword | Use Case | Returns Value? | Scope |
49+
|----------|---------------------------|----------------|----------------------------|
50+
| break | Old-style `switch` exit | No | Exits switch, not method |
51+
| return | Exit method completely | Yes | Whole method |
52+
| yield | Switch expression (Java 12+)| Yes | Current case → switch expr |
53+
54+
55+
6. Summary
56+
- Use `yield` in **multi-line switch expression cases** to provide a value.
57+
- Arrow (`->`) cases don’t need `yield` because they are single-expression.
58+
- `yield` is a safer, modern replacement for `break` in the context of expressions.

0 commit comments

Comments
 (0)