-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedSwitchStatement.java
More file actions
51 lines (45 loc) · 1.7 KB
/
EnhancedSwitchStatement.java
File metadata and controls
51 lines (45 loc) · 1.7 KB
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
// Enhanced Switch Statements in Java
/**
* Enhanced switch statements (Java 14 and later)
* make code more concise and readable.
*
* Key Features:
* - Arrow syntax (->) replaces traditional case + break.
* - Switch can return a value (called a "switch expression").
* - Multiple case labels can share the same action.
* - No need for 'break' — each case ends automatically.
*/
public class EnhancedSwitchStatement {
public static void main(String[] args) {
int day = 3;
// Example 1: Simple enhanced switch
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
case 4 -> System.out.println("Thursday");
case 5 -> System.out.println("Friday");
case 6, 7 -> System.out.println("Weekend!");
default -> System.out.println("Invalid day");
}
// Example 2: Switch as an expression (returns a value)
String dayType = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Unknown";
};
System.out.println("Day type: " + dayType);
// Example 3: Switch expression with block and yield
int month = 2;
int daysInMonth = switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
case 2 -> {
boolean isLeapYear = true;
yield (isLeapYear) ? 29 : 28;
}
default -> throw new IllegalArgumentException("Invalid month: " + month);
};
System.out.println("Days in month: " + daysInMonth);
}
}