【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
    }
}
相关推荐
ChinaRainbowSea12 分钟前
1. 初始 RabbitMQ 消息队列
java·中间件·rabbitmq·java-rabbitmq
lmryBC4922 分钟前
golang接口-interface
java·前端·golang
ゞ 正在缓冲99%…22 分钟前
leetcode75.颜色分类
java·数据结构·算法·排序
橘猫云计算机设计34 分钟前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
时光呢39 分钟前
JAVA常见的 JVM 参数及其典型默认值
java·开发语言·jvm
程序媛学姐1 小时前
SpringKafka错误处理:重试机制与死信队列
java·开发语言·spring·kafka
向阳2561 小时前
SpringBoot+vue前后端分离整合sa-token(无cookie登录态 & 详细的登录流程)
java·vue.js·spring boot·后端·sa-token·springboot·登录流程
XiaoLeisj1 小时前
【MyBatis】深入解析 MyBatis XML 开发:增删改查操作和方法命名规范、@Param 重命名参数、XML 返回自增主键方法
xml·java·数据库·spring boot·sql·intellij-idea·mybatis
风象南1 小时前
SpringBoot实现数据库读写分离的3种方案
java·spring boot·后端
振鹏Dong1 小时前
策略模式——本质是通过Context类来作为中心控制单元,对不同的策略进行调度分配。
java·策略模式