New feature of JDK 14: switch expression

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 function in JDK 14: JEP 361: switch expressions (standard).

In fact, switch has two new functions. One is that it can write case continuously, and the other is that switch can bring return value.

Write in front

When I was excited to create a package named switch, I suddenly found that Java classes were not created in idea.

After my repeated attempts and repeated renaming, I finally found the little secret hidden in it:

Java key word cannot be used in package name. Well, there have always been so many package names. Now I want to create a more popular one, but I found that there is such a rule.

So what are Java key words? Here it is.

Concatenation case

Let's take an example of the old version:

    @Test
    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 above example, we want to match all the weeks and print out the corresponding results. Wrote a lot of case statements, not pretty.

Take another example of the new version:

    @Test
    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 takes everything away.

Switch return value

Consider a case of assignment in switch:

    @Test
    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("这天没发见人!");
        }
    }

In the traditional way, we need to define a local variable and assign a value to this local variable in case.

Let's see how to replace it with the new version of switch:

    @Test
    public void newSwitchWithReturnValue(){
        int numLetters = switch (MONDAY) {
            case MONDAY,SUNDAY -> 6;
            case TUESDAY                -> 7;
            case THURSDAY,SATURDAY     -> 8;
            case WEDNESDAY              -> 9;
            default -> throw new IllegalStateException("这天没发见人!");
        };
    }

Is it very simple.

yield

In the case of the return value of switch above, if the expression after case is complex, you need to use curly braces to enclose it. In this case, we need to use yield to return the value to be returned.

    @Test
    public void withYield(){
        int result = switch (MONDAY) {
            case MONDAY: {
                yield 1;
            }
            case TUESDAY: {
                yield 2;
            }
            default: {
                System.out.println("不是MONDAY,也不是TUESDAY!");
                yield 0;
            }
        };
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>