Skip to content

Commit

Permalink
JEP 455: Primitive Types in Patterns, instanceof, and switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Oct 5, 2024
1 parent 1a1362a commit 37b585c
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ public static void main(String[] args) {
// 200 HTTP status code refers to a OK
// 403 HTTP status code refers to a Client Error
// 0 HTTP status code refers to a Unknown error

// An employee is calling me. class com.ibrahimatay.Employee
whoCallMe(new Employee());

// An employee is calling me. class com.ibrahimatay.Boss
whoCallMe(new Boss());

// Wrong number.
whoCallMe(null);
}

public static void whoCallMe(Human human) {
if (human instanceof Employee employee) {
System.out.printf("An employee is calling me. %1$s%n", employee.getClass());
return;
}

if (human instanceof Boss boss) {
System.out.printf("An employee is calling me. %1$s%n", boss.getClass());
return;
}

System.out.println("Wrong number.");
}

public static String getHTTPCodeDesc(int code) {
Expand All @@ -34,3 +57,7 @@ public static String getHTTPCodeDesc(int code) {
};
}
}

interface Human {}
class Employee implements Human {}
class Boss implements Human {}

0 comments on commit 37b585c

Please sign in to comment.