Skip to content

Commit 37b585c

Browse files
committed
JEP 455: Primitive Types in Patterns, instanceof, and switch
1 parent 1a1362a commit 37b585c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

java-23/src/main/java/com/ibrahimatay/JEP455PrimitiveTypesInPatternsInstanceofAndSwitch.java

+27
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ public static void main(String[] args) {
1414
// 200 HTTP status code refers to a OK
1515
// 403 HTTP status code refers to a Client Error
1616
// 0 HTTP status code refers to a Unknown error
17+
18+
// An employee is calling me. class com.ibrahimatay.Employee
19+
whoCallMe(new Employee());
20+
21+
// An employee is calling me. class com.ibrahimatay.Boss
22+
whoCallMe(new Boss());
23+
24+
// Wrong number.
25+
whoCallMe(null);
26+
}
27+
28+
public static void whoCallMe(Human human) {
29+
if (human instanceof Employee employee) {
30+
System.out.printf("An employee is calling me. %1$s%n", employee.getClass());
31+
return;
32+
}
33+
34+
if (human instanceof Boss boss) {
35+
System.out.printf("An employee is calling me. %1$s%n", boss.getClass());
36+
return;
37+
}
38+
39+
System.out.println("Wrong number.");
1740
}
1841

1942
public static String getHTTPCodeDesc(int code) {
@@ -34,3 +57,7 @@ public static String getHTTPCodeDesc(int code) {
3457
};
3558
}
3659
}
60+
61+
interface Human {}
62+
class Employee implements Human {}
63+
class Boss implements Human {}

0 commit comments

Comments
 (0)