Commit 0eb531c
committed
feat(Java12): add demo showcasing switch expressions and mention Shenandoah GC
What
- Added JAVA12 class in package Java12.
- Demonstrates new Java 12 features:
- Switch expressions (preview feature at the time).
- Example uses integer day to categorize as "Weekday", "Weekend", or "Invalid day".
- Uses new arrow (->) syntax and yield in block form.
- Mention of Shenandoah GC introduction as experimental low-pause garbage collector.
- Prints note about enabling it with JVM flag -XX:+UseShenandoahGC.
Why
- Illustrates concise switch expressions replacing verbose switch-case blocks.
- Demonstrates yield keyword in multi-statement branch.
- Educates about Shenandoah GC, a low-latency garbage collector added in Java 12.
How
- Declared int day = 3.
- Used switch expression:
- 1–5 → "Weekday".
- 6–7 → "Weekend".
- default → prints warning and yields "Invalid day".
- Printed result to stdout.
- Printed informational notes about Shenandoah GC usage.
Logic
- Inputs: day = 3.
- Outputs:
- "Day type: Weekday".
- Notes about Shenandoah GC.
- Flow:
1. switch expression matches day = 3.
2. Returns "Weekday".
3. Print result.
4. Print informational note about Shenandoah GC.
- Edge cases:
- day outside 1–7 triggers default branch with yield "Invalid day".
- Complexity / performance: O(1) branching.
- Concurrency / thread-safety: Purely local computation, no concurrency issues.
- Error handling: None required.
Real-life applications
- Switch expressions improve readability for mapping values, classification, or simple rules.
- Useful in parsers, state machines, or business logic.
- Shenandoah GC suited for applications needing predictable low-latency GC pauses (financial systems, gaming servers, etc.).
Notes
- switch expressions return values directly, making assignment concise.
- yield keyword is mandatory when block case is used with multiple statements.
- Shenandoah GC was experimental in Java 12 but later matured in subsequent releases.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f912ee6 commit 0eb531c
1 file changed
+21
-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 | + | |
0 commit comments