大家好,我是君哥。
新版任你发,我用 Java 8,这可能是当下 Java 开发者的真实写照。不过时代可能真的要抛弃 Java 8,全面拥抱 Java 17 了。
前些天,相信小伙伴们都注意到了,SpringBoot 发布了 3.0.0-M1 版本,并官宣全面拥抱 Java 17,下面的截图是 Release Notes 中的一段:
其他的新特性大家可以去 Github 查看。
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M1-Release-Notes/16c166b5fb4232998a8cfd3966c2851ec1eddf02
Spring Boot 3.0.0-M1 版本发布,也意味着 Spring Boot 进入 3.x 版本的迭代,同时也看到,3.x 版本开始 Spring Boot 要全面拥抱 Java 17 了。
凑巧的是,2022 年 2 月 11 日,Elastic 也发布了新版本 Elastic 8,这个版本跟上个版本间隔了 3 年。下面的截图是 Issue 79873:
可以看到,运行新的 Elasticsearch 版本需要 Java 17 了。
从 Java 8 以后,每个版本发布都增加了哪些新特性呢?我们一起来看一下。
String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";
if (obj instanceof String s) {
//这里可以使用 s 变量
} else {
//这里不能使用 s 变量
}
Object o;
switch (o) {
case null -> System.out.println("首先判断对象是否为空,走空指针逻辑等后续逻辑");
case String s -> System.out.println("判断是否为字符串,s:" + s);
case record p -> System.out.println("判断是否为Record类型: " + p.toString());
case int[] arr -> System.out.println("判断是否为数组,展示int数组的长度" + ia.length);
case Integer i -> System.out.println("判断是否为Intger对象,i:" + i);
case Student s -> System.out.println("判断是否为具体学生对象,student:" + s.toString());
case UserCommonService -> System.out.println("判断是否为普通用户实现类,然后走普通用户逻辑");
case UserVipService -> System.out.println("判断是否为vip用户实现类,然后走vip用户逻辑");
default -> System.out.println("Something else");
}
上面的版本只有 Java 11 和 Java 17 是 LTS(Long Term Support) 版本。
全面拥抱 Java 17 还没有真的到来,但已经在路上了,作为程序员的我们应该做准备了。