Details of new jdk13 features

In Java 13, the Test.java:Switch Expressions extends the previousJava 12 Switch Expressions by adding a new yieldkeyword to return a value from switch expression.

P.S :Switch expressions are a preview feature and are disabled by default.

1575097466031

1. No more value breaks!

1.1 The below Java 12 value breaks syntax is no longer compiled in Java 13, uses yieldinstead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	// value breaks are superseded by Java 13 'yield' statements. 
private static int Test(String demo) {
int result = switch (demo) {
case "a":
case "b":
break 1;
case "c":
break 2;
case "d":
case "e":
case "f":
break 3;
default:
break -1;
};
return result;
}
Copy

1.2 In Java 13, uses yield to return a value from switch

1
2
3
4
5
6
7
8
9
10
11
12
13
private static int Test(String demo) {
int result = switch (demo) {
case "a", "b":
yield 1;
case "c":
yield 2;
case "d", "e", "f":
yield 3;
default:
yield -1;
};
return result;
}

1.3 Return values vialabel rules or arrows like lambod expression from a switch expression (Java 12) is still supported in Java 13.

1
2
3
4
5
6
7
8
9
private static int Test(String demo) {
int result = switch (demo) {
case "a", "b" -> 1;
case "c" -> 2;
case "d", "e", "f" -> 3;
default -> -1;
};
return result;
}

2. Java 13 Switch Expressions

A full switch expression example in Java 13.

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
package com.chanzin.java13;

public class Test {

public static void main(String[] args) {

System.out.println(getValueViaYield("a"));
System.out.println(getValueViaYield("c"));
System.out.println(getValueViaYield("e"));
System.out.println(getValueViaYield("z"));

}

// Traditional switch
private static int getValueBefore12(String demo) {
int result;
switch (demo) {
case "a":
case "b":
result = 1;
break;
case "c":
result = 2;
break;
case "d":
case "e":
case "f":
result = 3;
break;
default:
result = -1;
}
;
return result;
}


private static int getValueMultipleLabels(String demo) {
int result;
switch (demo) {
case "a", "b":
result = 1;
break;
case "c":
result = 2;
break;
case "d", "e", "f":
result = 3;
break;
default:
result = -1;
}
;
return result;
}

// Java 13, value breaks are superseded by 'yield' statements
// Java 12, switch expression returning value via break
/*private static int getValueViaBreak(String mode) {
int result = switch (mode) {
case "a":
case "b":
break 1;
case "c":
break 2;
case "d":
case "e":
case "f":
break 3;
default:
break -1;
};
return result;
}*/

// Java 12, switch expression returning value via label rules (arrow)
private static int getValueViaLambod(String demo) {
int result = switch (demo) {
case "a", "b" -> 1;
case "c" -> 2;
case "d", "e", "f" -> 3;
default -> -1;
};
return result;
}

// Java 13, switch expression returning value via yield
private static int getValueViaYield(String demo) {
int result = switch (demo) {
case "a", "b":
yield 1;
case "c":
yield 2;
case "d", "e", "f":
yield 3;
default:
yield -1;
};
return result;
}

}
Copy

3. Enable preview features

3.1 A normal javac compile will prompts the following errors:

1575100008271

3.2 We need to enable the preview feature manually:

1
2
3
4
5
6
7
javac --enable-preview --release 13 Test.java


java --enable-preview Test


javap -c Test

1575100144648)1575100185623