当前位置:   article > 正文

【java17】java17新特性之switch表达式

【java17】java17新特性之switch表达式

Java17版本中switch表达式将允许switch有返回值,并且可以直接作为结果赋值给一个变量,等等一系列的变化。

下面有一个switch例子,依赖于给定的枚举值,执行case操作,故意省略break。

package com.morris.java17;

/**
 * switch省略break
 */
public class SwitchNotBreakDemo {
    public static void main(String[] args) {
        Month month = Month.APRIL;
        String season;
        switch (month) {
            case DECEMBER:
            case JANUARY:
            case FEBRUARY:
                season = "winter";
            case MARCH:
            case APRIL:
            case MAY:
                season = "spring";
            case JUNE:
            case JULY:
            case AUGUST:
                season = "summer";
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
                season = "autumn";
            default:
                season = "unknown";
        }
        System.out.println(season); // unknown
    }
}
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

运行结果如下:

unknown
  • 1

显然这不是期望的结果,因为我们需要在每个case里添加break防止所有的case都被执行。

package com.morris.java17;

/**
 * switch增加break
 */
public class SwitchBreakDemo {
    public static void main(String[] args) {
        Month month = Month.APRIL;
        String season;
        switch (month) {
            case DECEMBER:
            case JANUARY:
            case FEBRUARY:
                season = "winter";
                break;
            case MARCH:
            case APRIL:
            case MAY:
                season = "spring";
                break;
            case JUNE:
            case JULY:
            case AUGUST:
                season = "summer";
                break;
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
                season = "autumn";
                break;
            default:
                season = "unknown";
        }
        System.out.println(season); // unknown
    }
}
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

运行结果如下:

spring
  • 1

可以通过switch表达式来进行简化。将冒号:替换为箭头->,并且switch表达式不需要break。

package com.morris.java17;

/**
 * switch表达式
 */
public class SwitchExpressDemo {
    public static void main(String[] args) {
        Month month = Month.APRIL;
        String season;
        switch (month) {
            case DECEMBER, JANUARY, FEBRUARY -> season = "winter";
            case MARCH, APRIL, MAY -> season = "spring";
            case JUNE, JULY, AUGUST -> season = "summer";
            case SEPTEMBER, OCTOBER, NOVEMBER -> season = "autumn";
            default -> season = "unknown";
        }
        System.out.println(season); // spring
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

switch表达式也可以返回一个值,比如上面的例子我们可以让switch返回一个字符串来表示我们要打印的文本。需要注意在switch语句的最后要加一个分号。

package com.morris.java17;

/**
 * switch表达式带返回值的使用
 */
public class SwitchReturnDemo {
    public static void main(String[] args) {
        Month month = Month.APRIL;
        String season = switch (month) {
            case DECEMBER, JANUARY, FEBRUARY -> "winter";
            case MARCH, APRIL, MAY -> "spring";
            case JUNE, JULY, AUGUST -> "summer";
            case SEPTEMBER, OCTOBER, NOVEMBER -> "autumn";
            default -> "unknown";
        };
        System.out.println(season); // spring
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

如果你想在case里想做不止一件事,比如在返回之前先进行一些计算或者打印操作,可以通过大括号来作为case块,最后的返回值使用关键字yield进行返回。

package com.morris.java17;

/**
 * switch表达式yield的使用
 */
public class SwitchYieldDemo {
    public static void main(String[] args) {
        Month month = Month.APRIL;
        String season = switch (month) {
            case DECEMBER, JANUARY, FEBRUARY -> "winter";
            case MARCH, APRIL, MAY -> {
                System.out.println("current season is spring");
                yield "spring";}
            case JUNE, JULY, AUGUST -> "summer";
            case SEPTEMBER, OCTOBER, NOVEMBER -> "autumn";
            default -> "unknown";
        };
        System.out.println(season); // spring
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/814266
推荐阅读
相关标签
  

闽ICP备14008679号