Commit 9a15181
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
1 file changed
+58
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
0 commit comments