Skip to content

Commit 24aa3fc

Browse files
committed
Java 14: switch expression and pattern matching for instanceof
1 parent 3d49a5a commit 24aa3fc

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

java-11/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,3 @@
4646
* [Oracle's Z GC](https://wiki.openjdk.java.net/display/zgc/Main)
4747
* [Replacements for deprecated JPMS modules with Java EE APIs](https://stackoverflow.com/questions/48204141/replacements-for-deprecated-jpms-modules-with-java-ee-apis/48204154#48204154)
4848
* [Scripting Java 11, Shebang And All](https://blog.codefx.org/java/scripting-java-shebang/)
49-
*
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
public class PatternMatchingForInstanceOf {
2+
public static void main(String[] args) {
3+
Animal dog1 = new Dog("Bob");
4+
Animal c = new Cat();
5+
6+
// Old verbose way
7+
if (dog1 instanceof Dog) {
8+
// A cast just to manipulate the specific type
9+
Dog d1 = (Dog) dog1;
10+
play(d1);
11+
}
12+
13+
// New way
14+
if (dog1 instanceof Dog d1) {
15+
play(d1);
16+
}
17+
if (c instanceof Cat c1) {
18+
play(c1);
19+
}
20+
21+
Animal dog2 = new Dog("Bob");
22+
System.out.println("\nAre both equal? " + dog1.equals(dog2));
23+
}
24+
25+
static void play(Dog d) {
26+
System.out.println("Playing with Dog...");
27+
d.makeSound();
28+
}
29+
30+
static void play(Cat c) {
31+
System.out.println("Playing with Cat...");
32+
c.makeSound();
33+
}
34+
}
35+
36+
class Animal {
37+
void makeSound() {
38+
System.out.println("Generic sound");
39+
}
40+
}
41+
42+
class Cat extends Animal {
43+
void makeSound() {
44+
System.out.println("Miau");
45+
}
46+
}
47+
48+
class Dog extends Animal {
49+
// here we assume that are equal when they have the same name
50+
private String name;
51+
52+
Dog(String name) {
53+
this.name = name;
54+
}
55+
56+
void makeSound() {
57+
System.out.println("Au au");
58+
}
59+
60+
/*
61+
* Old way where we need to cast after the `instanceof`.
62+
*/
63+
public boolean equalsOldWay(Object o) {
64+
// need to check if is the same type
65+
if (!(o instanceof Dog))
66+
return false;
67+
// then we cast to work with this type
68+
Dog d1 = (Dog) o;
69+
// only now we can performe our [business] logic to compare
70+
return d1.name.equals(d1.name);
71+
}
72+
73+
public boolean equals(Object o) {
74+
// more straightforward
75+
return (o instanceof Dog d1)
76+
&& d1.name.equals(d1.name);
77+
}
78+
}

java-14/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Java 14
22

3+
To run each example use: `java --enable-preview --source 14 <FileName.java>`
4+
35
## Features
46

57
### Language

java-14/SwitchExpressions.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* We can run with:
3+
* java <FileName.java>
4+
*/
5+
public class SwitchExpressions {
6+
public static void main(String[] args) {
7+
// Now switch expressions are final o/
8+
9+
FeaturePhase switchWithPatternMatching = FeaturePhase.FINAL;
10+
11+
var message = switch (switchWithPatternMatching) {
12+
case FINAL -> "No longer need to use `--preview`";
13+
case PREVIEW -> "Still needs to use `--preview`";
14+
};
15+
16+
System.out.println("Switch expressions with pattern matching: " + message);
17+
}
18+
}
19+
20+
enum FeaturePhase {
21+
PREVIEW,
22+
FINAL
23+
}

0 commit comments

Comments
 (0)