JDK23支持switch传入long类型

前言

JDK23以前版本,不支持使用Long或者long类型传入,但是JDK23支持该类型了,但是只是预览属性

switch使用

JDK23以往版本中,switch这么写,会报错

csharp 复制代码
public class Test {

    public static void main(String[] args) {
        Long a = 1L;
        switch (a) {
            case 1L:
                System.out.println(1);
                break;
            case 2L:
                System.out.println(2);
                break;
        }
    }
}

会报

但是JDK23版本支持long类型了

csharp 复制代码
public class LongDemo {

    public static void main(String[] args) {
        long a = 1L;
        switch (a) {
            case 1L -> System.out.println(1);
            case 2L -> System.out.println(2);
            default -> System.out.println(3);

        }
    }
}

输出

备注该功能必须开启预览属性

总结

JDK23中,JEP 455 的预览特性中,switch 全面支持所有原始类型,包括 byte, short, char, int, long, float, double, boolean

相关推荐
咖啡八杯1 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
lizhongxuan1 天前
AI Agent 上下文压缩利器 Headroom
后端
Csvn1 天前
SSH 远程管理与安全加固 — 运维的守门之道
后端
IT_陈寒1 天前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
菜鸟谢1 天前
Rust 智能指针完整详解
后端
菜鸟谢1 天前
Rust 函数完整知识点详解
后端
爱勇宝1 天前
淡泊名利之前,先承认我们都很焦虑
前端·后端·程序员
菜鸟谢1 天前
Rust 闭包(Closure)完整详解
后端
ServBay1 天前
如何利用本地技术栈构建 0 成本 AI SaaS 雏形
后端·aigc·ai编程
菜鸟谢1 天前
Rust 集合 + 迭代器完整详解
后端