JEP 406 is a Java Enhancement Proposal (JEP) that was issued by the OpenJDK Community, the open-source organization responsible for developing the Java Development Kit (JDK). JEP 406 specifically proposes the addition of a new feature to the Java language called “pattern matching for switch,” which allows developers to use a more concise and readable syntax when checking the type of an object at runtime. The syntax for this feature is similar to the syntax used for pattern matching in other programming languages, such as functional languages like Haskell.
Here is an example of how the “pattern matching for switch” feature might be used:
Java 14 – Switch Expressions Share on XSyntax –
1 2 3 4 | if (obj instanceof String s) { // s is now a String System.out.println(s.length()); } |
Example – If Else – before Java 17
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 | public class JEP406_Example1 { public static void main(String[] args) { System.out.println(formatter(17)); System.out.println(formatter(17L)); System.out.println(formatter(17.0)); System.out.println(formatter("Java 17")); } static String formatter(Object o) { String formatted = "unknown"; if (o instanceof Integer i) { formatted = String.format("int %d", i); } else if (o instanceof Long l) { formatted = String.format("long %d", l); } else if (o instanceof Double d) { formatted = String.format("double %f", d); } else if (o instanceof String s) { formatted = String.format("String %s", s); } return formatted; } } |
Example – If Else – Java 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class JEP406_Example2 { public static void main(String[] args) { System.out.println(formatter(17)); System.out.println(formatter(17L)); System.out.println(formatter(17.0)); System.out.println(formatter("Java 17")); } static String formatter(Object o) { return switch (o) { case Integer i -> String.format("int %d", i); case Long l -> String.format("long %d", l); case Double d -> String.format("double %f", d); case String s -> String.format("String %s", s); default -> o.toString(); }; } } |
Example – Pattern matching and null – before Java 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class JEP406_Example3 { public static void main(String[] args) { check("Tik"); // Dangerous check("Tokker"); // Ok check(null); // Fail check(""); // Ok } static void check(String s) { if (s == null) { System.out.println("Fail"); return; } switch (s) { case "Tik", "Tok" -> System.out.println("Dangerous"); default -> System.out.println("Ok"); } } |
Example – Pattern matching and null – Java 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class JEP406_Example4 { public static void main(String[] args) { checkwithJava17("Tik"); // Dangerous checkwithJava17("Tokker"); // Ok checkwithJava17(null); // Fail checkwithJava17(""); // Ok } static void checkwithJava17(String s) { switch (s) { case null -> System.out.println("Fail"); case "Tik", "Tok" -> System.out.println("Dangerous"); default -> System.out.println("Ok"); } } |
NOTE:
If you want to go deep into learning JEP 406 and want to see more examples, then please visit JEP 406: Pattern Matching for switch (Preview)
Java 14 – Switch Expressions Share on Xconvert intstream to integer array
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: