JDK12~17的新特性:Switch增强,优化NPE,文本块,instanceof增强,record,sealed

JDK14

switch语句的增强:

类似lambda的语法糖,不需要再写break了。提供yield实现返回值

其中switch类型匹配属于预览,正常情况下是关闭的

复制代码
public class EnhanceSwitch {
    public static void main(String[] args) {
        oldVersion();

        newVersion();
    }

    private static void oldVersion() {
        int num = 1;
        switch (num) {
            case -1:
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            default:
                System.out.println("other");
                break;
        }
    }
    //1.不再使用break
    //2.支持返回值,yield关键字返回block内的数据
    //3.支持多个case合并

    private static void newVersion() {
        int num = -1;
        int newNum = switch (num) {
            case -1, 1 -> {
                System.out.println("one");
                yield 1;
            }
            case 2 -> {
                System.out.println("two");
                yield 2;
            }
            default -> {
                System.out.println("other");
                yield 0;
            }
        };
    }


}
复制代码

优化NPE信息

jdk14之后会明确告诉你哪一行的具体哪个值发生了空指针异常

 

 

JDK15

文本块

像python的注释一样使用六个双引号即可生成文本块,减少了json块的复杂拼接,可使用string.formatted()为%s填充参数

在JDK17中新增了两个转义字符    \s:添加一个空格(在行尾手动无法实现)   \:让两行拼接在一起(其实手动也可以实现,但是使用这个在某些场景可以看起来更美观一些)

复制代码
public class EnhanceTextBlock {
    public static void main(String[] args) {
        String name = "blaze";
        String sex = "man";
        String info = """
                name: %s \s\s\
                sex: %s
                """.formatted(name, sex);
        System.out.println(info);
    }
}
复制代码

输出结果:

 

 

JDK16

instanceof增强:

instanceof的时候指定好new的对象的name即可,不需要在语句内再new对象

复制代码
public class EnhanceInstanceof {
    public static void main(String[] args) {
        oldVersion();
        newVersion();
    }

    static void oldVersion(){
        Object o="abc";
        if(o instanceof String){
            String i=(String) o;
            System.out.println(i);
        }
    }
    static void newVersion(){
        Object o="abc";
        if(o instanceof String s){
            System.out.println( s.charAt(0));
        }
    }
}
复制代码

 

record关键字

提供一种妄图代替lombok的关键字:可以快速实现一个javabean,只需要在构造时写清变量并赋值。注意,这些变量都是final的,不能再次赋值,不过可以对list这类集合新删改内部元素。

问题是一旦使用record后内部就不能再次赋值,也就是说适用于不变量的场景,此外它适用于处理一些中间的内部类,传递给下一个方法时比map让代码更清晰

编译前的.java

public record People(Integer id,String name, String sex) {
    public People{
        if(!sex.equals("男")&&!sex.equals("女")){
            throw new RuntimeException("在中国这样的性别不合法");
        }
    }
}

编译后的.class

复制代码
public record People(Integer id, String name, String sex) {
    public People(Integer id, String name, String sex) {
        if (!sex.equals("男") && !sex.equals("女")) {
            throw new RuntimeException("在中国这样的性别不合法");
        } else {
            this.id = id;
            this.name = name;
            this.sex = sex;
        }
    }

    public Integer id() {
        return this.id;
    }

    public String name() {
        return this.name;
    }

    public String sex() {
        return this.sex;
    }
}
复制代码

如何使用

public class TestRecord {
    public static void main(String[] args) {
        People record = new People(1,"LJK", "男");
        System.out.println(record.sex());
    }
}

 

 

JDK17

密封类

关键字sealed , permits , non-sealed。值得注意的是,一旦使用密封类,那么子类就需要和父类在同一个目录层级中不然就会报错:Class is not allowed to extend sealed class from another package

// sealed 是jdk17的新特性,密封类
// 通过sealed修饰的抽象类/接口,只能被指定的实现类继承
public sealed abstract class Person permits Man,Woman {
}
// final关键字继承seal后,该子类不能被其他类继承
public final class Man extends Person {
}
// 使用non-sealed关键字,表示该类可以被继承
public non-sealed class Woman extends Person{
}

 

posted @   天启A  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示