-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jdk14.java
117 lines (103 loc) · 3.81 KB
/
Jdk14.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package samples.jdk14;
import java.time.DayOfWeek;
public class Jdk14 {
public static void main(String[] args) {
//oldSwitch();
switchExpressionSingleLine();
switchExpressionExaustiveWithEnum();
//switchExpressionMultiLine()
helpfulNullPointerException();
}
/**
* Shows how the NullPointerException message was improved
* to make it clear totally where the exception ocurred.
* @see <a href="https://openjdk.org/jeps/358">JEP 358</a>
*/
private static void helpfulNullPointerException() {
ClassA a = null;
ClassX x = null;
try {
a.b = x.y;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Shows utilization example of the new Switch Expression feature,
* where each case instruction has only one line of code.
* Switch Expressions enable the switch to yield ("return") a value
* which can be assigned to a variable.
* Different from the tradditional switch, such a variable can be
* final because we have just a single variable assignement.
*
* @see <a href="https://openjdk.org/jeps/361">JEP 361</a>
*/
private static void switchExpressionSingleLine() {
final int month = (int)(Math.random()*12 + 1);
final int days = switch (month) {
case 2 -> 28;
case 4, 6, 9, 11 -> 30;
default -> 31;
};
System.out.printf("Month: %d Days: %d%n", month, days);
}
/**
* Show how Switch Expression can be exahustive when using
* together with enums. Checks if all possible values for an enum
* were covered by the switch.
*
* @see <a href="https://openjdk.org/jeps/361">JEP 361</a>
*/
private static void switchExpressionExaustiveWithEnum() {
final var weekDay = DayOfWeek.of((int)(Math.random()*7 + 1));
final var categoryName = switch (weekDay) {
case SATURDAY, SUNDAY -> "Weekend";
default -> "Weekday";
};
System.out.printf("%s is %s%n", weekDay, categoryName);
}
/**
* Shows how to use a Switch Expression with multiple intructions for
* some cases.
*
* @see <a href="https://openjdk.org/jeps/361">JEP 361</a>
*/
private static void switchExpressionMultiLine() {
final int month = (int)(Math.random()*12 + 1);
final int days = switch (month) {
case 2 -> {
System.out.println("We are not considering that for leap years February has 29 days.");
yield 28;
}
case 4, 6, 9, 11 -> 30;
default -> 31;
};
System.out.printf("Month: %d Days: %d%n", month, days);
}
/**
private static void switchExpressionNonExaustive() {
//This code DOES NOT COMPILE, since using Switch Expression,
//the compiler che ks if all options were covered by the case instructions.
//In this example, months with 31 days weren't included in the switch,
//neither a default clause.
//This way, the days variable wouldn't have a value for months 1, 3, 5, 7, 8, 10 and 12.
// Código deste método NÃO COMPILA, pois com Switch Expression, o compilador verifica
final int month = (int)(Math.random()*12 + 1);
final int days = switch (month) {
case 2 -> 28;
case 4, 6, 9, 11 -> 30;
};
System.out.printf("Month: %d Days: %d%n", month, days);
}
*/
private static void oldSswitch() {
final int month = (int)(Math.random()*12 + 1);
int days;
switch (month) {
case 2: days = 28; break;
case 4, 6, 9, 11: days = 30; break;
default: days = 31; break;
}
System.out.printf("Month: %d Days: %d%n", month, days);
}
}