Skip to content

Commit 0eb531c

Browse files
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

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Java12;
2+
3+
public class JAVA12 {
4+
public static void main(String[] args) {
5+
// Switch Expressions (Preview Feature)
6+
int day = 3;
7+
String dayType = switch (day) {
8+
case 1, 2, 3, 4, 5 -> "Weekday";
9+
case 6, 7 -> "Weekend";
10+
default -> {
11+
System.out.println("Unknown day, assigning default...");
12+
yield "Invalid day";
13+
}
14+
};
15+
16+
System.out.println("Day type: " + dayType);
17+
18+
System.out.println("\n[Note] Shenandoah GC was introduced as an experimental GC in Java 12.");
19+
System.out.println("Use JVM flags like: -XX:+UseShenandoahGC to test its low-pause capabilities.");
20+
}
21+
}

0 commit comments

Comments
 (0)