当前位置: 代码网 > it编程>编程语言>Java > JDK23中instanceof的使用小结

JDK23中instanceof的使用小结

2025年08月06日 Java 我要评论
前言在jdk23以前版本,instanceof关键字是不支持拓展基本类型,允许在所有模式上下文中使用原始类型,包括 long、float、double 和boolean等,但目前是预览属性instan

前言

jdk23以前版本,instanceof关键字是不支持拓展基本类型,允许在所有模式上下文中使用原始类型,包括 longfloatdoubleboolean等,但目前是预览属性

instanceof使用

jdk23以前是这么使用的,要使用引用类型

public class instanceof1demo {


    public static void main(string[] args) {
        object data = 1;
        if (data instanceof integer) {
            system.out.println(data);
        } else {
            system.out.println(2);
        }
    }
}

输出结果为

使用jdk23中的instanceof要先开启预览属性

public class instanceof1demo {


    public static void main(string[] args) {
        object data = 1;
        if (data instanceof int str) {
            system.out.println(str);
        } else {
            system.out.println(2);
        }
    }
}

输出结果为

总结

jdk23中,jep 455 的预览特性中,instanceof 全面支持所有原始类型,包括 byteshortcharintlongfloatdoubleboolean,但是属于预览属性,没有转正,使不使用就看个人选择

到此这篇关于jdk23中instanceof的使用小结的文章就介绍到这了,更多相关jdk23 instanceof内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com