Skip to content

Commit c39ea0d

Browse files
committed
improved examples of switch expression in Java 14;
1 parent 42807dc commit c39ea0d

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ A project to explore more about the new features of Java 8, 9, ...
1414
* Virtual Thread :rocket:
1515
* Pattern matching for `switch` (preview 3)
1616
* Vector API (fourth incubator)
17+
* Record pattern
18+
* Structured concurrency
1719

1820
* [Java 18](java-18/) (Mar, 2022)
1921
* UTF-8 by Default
@@ -24,6 +26,7 @@ A project to explore more about the new features of Java 8, 9, ...
2426

2527
* [Java 17](java-17/) (Sep, 2021)
2628
* Sealed classes (standard)
29+
* Pattern matching for `switch` (preview)
2730
* Enhanced Pseudo-Random Number Generator
2831
* Deprecate the Applet API for Removal
2932
* New macOS rendering for Java 2D API

java-12/SwitchExpressionExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void inferringType() {
8686
var mood = Incomes.Good;
8787

8888
// compiler infers the most specific supertype
89-
// of String and IllegalArgumentException = Serializable
89+
// of String and IllegalArgumentException is Serializable
9090
// Serializable whichType // we can only use its supertype
9191
var whichType = switch (mood) {
9292
case Good -> "All right";

java-13/SwitchExpressionTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*
2-
* Same example from Java 12 but with syntax update.
1+
/**
2+
* Same example from Java 12 but with syntax update (changed `break` to `yield`).
33
*/
44
public class SwitchExpressionTests {
55
public static void main(String ... args) {

java-14/SwitchExpressions.java

+73-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
public class SwitchExpressions {
66
public static void main(String[] args) {
77
// Now switch expressions are final o/
8+
// kinds of switch case statements
9+
System.out.println(oldWay(Mood.BAD));
10+
System.out.println(lambdaWay(Mood.GOOD));
11+
System.out.println(mixingSwitchKinds(Mood.REGULAR));
812

913
FeaturePhase switchWithPatternMatching = FeaturePhase.FINAL;
1014

@@ -15,9 +19,77 @@ public static void main(String[] args) {
1519

1620
System.out.println("Switch expressions with pattern matching: " + message);
1721
}
22+
23+
static String oldWay(Mood mood) {
24+
// old way (set a var or return method value)
25+
switch (mood) {
26+
// we use lambda here, swith isn't used in a statement
27+
// error: not a statement
28+
// case GOOD -> "All right";
29+
30+
// can mix kinds of switch
31+
// error: different case kinds used in the switch
32+
// case GOOD -> { return "All right"; }
33+
34+
case GOOD: return "All right";
35+
case REGULAR: return "Can do better";
36+
default: return "Let's improve!";
37+
}
38+
}
39+
40+
static String lambdaWay(Mood mood) {
41+
return switch (mood) {
42+
case GOOD -> {
43+
// yield can only be used in a switch statement
44+
yield "All right";
45+
}
46+
// lambda without block requires semi-colon
47+
case REGULAR -> "Can do better";
48+
default -> "Let's improve!";
49+
};
50+
}
51+
52+
static String mixingSwitchKinds(Mood mood) {
53+
String message;
54+
switch (mood) {
55+
// here we can return because isn't a switch statement
56+
case BAD -> {
57+
return "Not today";
58+
}
59+
// we still can change vars
60+
default -> {
61+
message = "We still can go";
62+
}
63+
}
64+
65+
// switch used in a statement
66+
var result = switch (mood) {
67+
// we cannot mix return here (will cause `not a statement` in the following cases)
68+
69+
case GOOD -> {
70+
// we cannot return when switch is used as statement
71+
// error: attempt to return out of a switch expression
72+
// return "All right";
73+
yield "All right";
74+
}
75+
76+
// can mix kinds of switch
77+
// error: different case kinds used in the switch
78+
// default: return "Still can improve";
79+
80+
default -> "Still can improve";
81+
};
82+
return result;
83+
}
1884
}
1985

2086
enum FeaturePhase {
2187
PREVIEW,
2288
FINAL
23-
}
89+
}
90+
91+
enum Mood {
92+
GOOD,
93+
REGULAR,
94+
BAD
95+
}

0 commit comments

Comments
 (0)