brief introduction
The new feature of switch has a long history. It was introduced as a preview function as early as JDK 12, and finally became an official version of the function in JDK 14: JEP 361: Switch Expressions (Standard).
In fact, there are two new functions of switch, one is that case can be linked, the other is that switch can bring return value.
Write in front
When I was interested in creating a package named after switch, I suddenly found that I could not create java classes in IDEA.
After repeated attempts and name changes, I finally found the secret hidden in it:
java key word cannot be used in package name. Well, there have been so many package names all the time. Now I want to create a more fashionable one, but I find that there is such a rule.
What are the Java key words? Here it is.
Linking case
Let's look at an example of an old version:
[@Test](https://my.oschina.net/azibug) public void useOldSwitch(){ switch (MONDAY) { case MONDAY: case FRIDAY: case SUNDAY: System.out.println(6); break; case TUESDAY: System.out.println(7); break; case THURSDAY: case SATURDAY: System.out.println(8); break; case WEDNESDAY: System.out.println(9); break; } }
In the example above, we want to match all the weeks and print out the corresponding results. Many case statements have been written, which is not beautiful.
Take a look at the new version:
[@Test](https://my.oschina.net/azibug) public void useNewSwitch(){ switch (MONDAY) { case MONDAY, FRIDAY, SUNDAY -> System.out.println(6); case TUESDAY -> System.out.println(7); case THURSDAY, SATURDAY -> System.out.println(8); case WEDNESDAY -> System.out.println(9); } }
A beautiful ligature, take everything away.
Note that the switch statement here does not return a value, so a default statement is not required.
switch return value
Consider a case where a value is assigned in switch:
[@Test](https://my.oschina.net/azibug) public void oldSwitchWithReturnValue(){ int numLetters; switch (MONDAY) { case MONDAY: case FRIDAY: case SUNDAY: numLetters = 6; break; case TUESDAY: numLetters = 7; break; case THURSDAY: case SATURDAY: numLetters = 8; break; case WEDNESDAY: numLetters = 9; break; default: throw new IllegalStateException("No one was seen this day!"); } }
In the traditional way, we need to define a local variable and assign a value to the local variable in case.
Let's see how to use the new switch replacement:
[@Test](https://my.oschina.net/azibug) public void newSwitchWithReturnValue(){ int numLetters = switch (MONDAY) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> throw new IllegalStateException("I didn't see anyone this day!"); }; }
Is it very simple.
Note that a default operation is required here, otherwise a compilation error will be reported. Because there may be values that are not traversed.
yield
In the case of the above switch return value, if the expression after the case is complex, it needs to be enclosed by braces. In this case, we need to use yield to return the value to be returned.
[@Test](https://my.oschina.net/azibug) public void withYield(){ int result = switch (MONDAY) { case MONDAY: { yield 1; } case TUESDAY: { yield 2; } default: { System.out.println("No MONDAY,Neither TUESDAY!"); yield 0; } }; }
summary
This paper introduces the new features of switch in JDK14. It's the only new feature in the official version
Examples of this article https://github.com/ddean2009/learn-java-base-9-to-20
Author of this article: the flydean program
Link to this article: http://www.flydean.com/jdk-14-switch/
Source: flydean's blog
Welcome to my official account: the procedures, the more wonderful things waiting for you!