Skip to content

Commit a866547

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 0eb531c commit a866547

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class JAVA14 {
2+
public static void main(String[] args) {
3+
// Switch Expressions - Now a Standard Feature
4+
int day = 6;
5+
String dayType = switch (day) {
6+
case 1, 2, 3, 4, 5 -> "Weekday";
7+
case 6, 7 -> "Weekend";
8+
default -> {
9+
System.out.println("Invalid day detected.");
10+
yield "Unknown";
11+
}
12+
};
13+
System.out.println("Day type: " + dayType);
14+
15+
// Multi-line block with yield
16+
int score = 85;
17+
String grade = switch (score / 10) {
18+
case 10, 9 -> "A";
19+
case 8 -> "B";
20+
case 7 -> "C";
21+
case 6 -> "D";
22+
default -> {
23+
yield "F";
24+
}
25+
};
26+
System.out.println("Grade: " + grade);
27+
}
28+
}

0 commit comments

Comments
 (0)