Java 17 Recipes


-16. Pattern Matching for switch (Preview)


Download 3.2 Mb.
Pdf ko'rish
bet48/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   44   45   46   47   48   49   50   51   ...   245
Bog'liq
Java 17 Recipes

2-16. Pattern Matching for switch (Preview)
 Problem
You want to efficiently test the same variable against a different number of values.
 Solution
The switch command will not support this feature until Java 17, and as a preview. For 
now, you use a chain of if-else tests. The following is an example.
public static void main(String[] args){
Object inputObject = 500L;
String formattedObject =”input object is not formatted corretly!”;
if (inputObject instanceof Integer i) {
formattedObject = String.format("Integer %d", i);
} else if (inputObject instanceof String s) {
formattedObject = String.format("String %s", s);
} else if (inputObject instanceof Long l) {
formattedObject = String.format("Long %d", l);
} else if (inputObject instanceof Double d) {
formattedObject = String.format("Double %f", d);
}
Chapter 2 enhanCements from Java 9 through Java 17 


77
The code can be rewritten smartly and reliably in Java 17, as follows.
Object inputObject = 500L;
String formattedObject = switch (inputObject ) {
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 -> inputObject .toString();
};
Open a terminal window for compiling and executing command lines. The following 
is the output.
long 500
 How It Works
Java 17 introduces the pattern matching for the switch command because the if…else 
code is complex and not optimizable, while switch is good for pattern matching. In fact, 
a case label matches the selector expression if the value matches the pattern.
The written patterns in switch statements are a preview feature and are disabled 
by default. You can compile ad execute from the terminal window. Use the following 
command to enable the patterns preview feature in switch statements.
--enable-preview
Use the following command to compile the specified Java 17 SE release.
–release 17
javac --enable-preview --release 17 PattSwitchEx.java
If you also use the -Xlint command for details, the compiler warns about the 
preview feature patterns because they may be removed in a future release.
When you execute the compiled class, you can simply write the following.
java --enable-preview PattSwitchEx
For more information on pattern matching for switch, see the documentation at 
https://openjdk.java.net/jeps/406
.
Chapter 2 enhanCements from Java 9 through Java 17 


78
2-17. Summary
This chapter covered a handful of the new features and enhancements through Java 17.
While certainly not a complete listing of new features, this chapter delved into a few 
of the most anticipated features, including vectors and pattern matching for switch. 
The entire book should be read to gain a more complete knowledge of new features. 
However, this chapter gave you a taste of what is to come.
Chapter 2 enhanCements from Java 9 through Java 17 


79
© Josh Juneau, Luciano Manelli 2022 
J. Juneau and L. Manelli, Java 17 Recipes
https://doi.org/10.1007/978-1-4842-7963-2_3

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   44   45   46   47   48   49   50   51   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling