一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情。
Java8于2014年3月18日发布,截止到2022年4月6日,当前最新发行版本是Java18。版本17、11和8是目前支持的长期支持(LTS)版本。这篇文章带领大家回顾从Java 8 开始每个版本的特性,小板凳坐好,发车了!想看上一篇文章,点击这儿面试反杀 | 请谈谈Java8-18引入的新特性(五)
Java 16新特性
Stream.toList()
从 Java 16中,我们可以直接将结果从流收集到一个列表中,而不用使用 Collectors.toList ()。
List<Integer> collectedList = locList.stream().filter( l -> l >=2).collect(Collectors.toList());
复制代码
上面的代码将产生与下面给出的结果相同的结果:
List<Integer> collectedListNew = locList.stream().filter( l -> l >=2).toList();
复制代码
Invoke Default Methods From Proxy Instances
作为 Interfaces 中缺省方法的增强,随着 Java 16的发布,Java.lang.reflect 得到了支持。InvocationHandler 通过使用反射的动态代理调用接口的默认方法。
来看一个简单的默认方法例子:
interface HelloWorld {
default String hello() {
return "world";
}
}
复制代码
通过这个改进,我们可以使用反射来调用该接口的代理的默认方法:
Object proxy = Proxy.newProxyInstance(getSystemClassLoader(), new Class<?>[] { HelloWorld.class },
(prox, method, args) -> {
if (method.isDefault()) {
return InvocationHandler.invokeDefault(prox, method, args);
}
// ...
}
);
Method method = proxy.getClass().getMethod("hello");
assertThat(method.invoke(proxy)).isEqualTo("world");
复制代码
Day Period Support
日期时间格式化程序的一个新添加符号“ b”,它提供了 am/pm 格式的替代:
LocalTime date = LocalTime.parse("15:25:08.690791");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B");
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
复制代码
我们得到的不是“下午3点”,而是“下午3点”。我们还可以分别使用“ b”、“ BBBB”或“ bbb”DateTimeFormatter 模式来处理短样式、完整样式和窄样式。
当然还有其它的新特性,包括但不限于:
- Vector API Incubator
- New Additions to Records
Java 17新特性
Java17是一个长期支持(LTS)版本,于2021年9月14日达到 General Availability,其含有有14个 JEP条目。
JEP 306: Restore Always-Strict Floating-Point Semantics
这个 JEP 主要用于数值敏感的程序,主要用于科学目的; 它再次使默认的浮点操作变得严格,或者叫做 Strictfp,以确保每个平台上的浮点计算得到相同的结果。
自 Java 1.2以来,我们需要关键字 strictfp 来启用严格的浮点计算。默认的浮点计算从严格改为略微不同的浮点计算(避免过热问题)。
Java17将前 java1.2严格的浮点计算恢复为默认值,这意味着关键字 strictfp 现在是可选的。
JEP 356: Enhanced Pseudo-Random Number Generators
这个 JEP 引入了一个叫做 RandomGenerator 的新接口,使得未来的 PRNG 算法更容易实现和使用。这个接口的伪随机数生成器是:
package java.util.random;
public interface RandomGenerator {
//...
}
复制代码
下面的示例使用新的 java17随机发生器工厂获得著名的 Xoshiro256PlusPlus PRNG 算法来生成特定范围内的随机整数,0-10。
package com.mine.java17.jep356;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class JEP356 {
public static void main(String[] args) {
RandomGenerator randomGenerator = RandomGeneratorFactory.of("Xoshiro256PlusPlus").create(999);
System.out.println(randomGenerator.getClass());
int counter = 0;
while(counter<=10){
// 0-10
int result = randomGenerator.nextInt(11);
System.out.println(result);
counter++;
}
}
}
复制代码
下面的代码生成所有的 Java 17 PRNG 算法。
RandomGeneratorFactory.all()
.map(fac -> fac.group()+ " : " +fac.name())
.sorted()
.forEach(System.out::println);
复制代码
17还重构了遗留的随机类,比如 Java.util.Random、 SplittableRandom 和 securierandom,以扩展新的随机生成器接口。
JEP 382: New macOS Rendering Pipeline
苹果在 macOS 10.14发布版(2018年9月)中不推荐 OpenGL 渲染库,而是支持新的 Metal 框架,以获得更好的性能。
这个 JEP 改变了 macOS 的 java2d (如 Swing GUI)内部呈现管道,从 Apple OpenGL API 改为 Apple Metal API; 这是一个内部改变; 没有新的 java2d API,也没有改变任何现有的 API。
JEP 409: Sealed Classes
Java 15 和Java 16 引入了Sealed Classes作为预览功能。当前这个JEP宣布了封闭类已经成为了标准特性。密封类和接口控制或者限制了谁能成为其子类型。
当然还有其它的新特性,包括但不限于:
- JEP 391: macOS/AArch64 Port
- JEP 398: Deprecate the Applet API for Removal
- JEP 403: Strongly Encapsulate JDK Internals
- JEP 406: Pattern Matching for switch (Preview)
- JEP 407: Remove RMI Activation
- JEP 410: Remove the Experimental AOT and JIT Compiler
- JEP 411: Deprecate the Security Manager for Removal
- JEP 412: Foreign Function & Memory API (Incubator)
未完待续,下面继续讲各个版本的新特性,敬请期待!
终章,2022年3月22日发布的Java 18。