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

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

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

java 复制代码
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
    }
}

运行结果如下:

shell 复制代码
unknown

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

java 复制代码
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
    }
}

运行结果如下:

shell 复制代码
spring

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

java 复制代码
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
    }
}

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

java 复制代码
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
    }
}

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

java 复制代码
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
    }
}
相关推荐
让学习成为一种生活方式几秒前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
晨曦_子画6 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
南宫生28 分钟前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
Heavydrink42 分钟前
HTTP动词与状态码
java
ktkiko111 小时前
Java中的远程方法调用——RPC详解
java·开发语言·rpc
计算机-秋大田1 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
神里大人1 小时前
idea、pycharm等软件的文件名红色怎么变绿色
java·pycharm·intellij-idea
小冉在学习1 小时前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论
代码之光_19802 小时前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi2 小时前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet