Java 12 新特性 (EOL)
warning
生命周期结束时间(EOL): 2019/12/31
官方发布介绍页面:https://openjdk.org/projects/jdk/12/
1. Switch 表达式 (第一次预览)
Switch 表达式允许在 switch 语句中使用表达式,并返回值。
int num = 2;
String result = switch (num) {
case 1 -> "one";
case 2 -> "two";
case 3 -> "three";
default -> "unknown";
};
System.out.println(result); // two
2. Shenandoah 垃圾回收器 (实验性)
引入了低暂停时间的 Shenandoah 垃圾回收器。
// 使用命令行参数来启用 Shenandoah GC
// -XX:+UseShenandoahGC
3. Microbenchmark Suite
引入了一个新的微基准测试套件。
4. JVM 常量 API
引入了新的 API 来模型化 JVM 常量池中的常量。
5. One AArch64 Port, Not Two
合并了两个 AArch64 端口。
6. Default CDS Archives
默认启用类数据共享 (CDS) 存档。
// 使用命令行参数来启用 CDS
// -Xshare:on
7. Abortable Mixed Collections for G1
G1 垃圾回收器支持可中止的混合收集。
// 使用命令行参数来启用可中止的混合收集
// -XX:+AbortableMixedCollections
8. Promptly Return Unused Committed Memory from G1
G1 垃圾回收器可以及时返回未使用的已提交内存。
// 使用命令行参数来启用此功能
// -XX:G1PeriodicGCInterval=<interval>
9. FileSystems.newFileSystem() Method
FileSystems 类增加了 newFileSystem 方法。
Path path = Paths.get("test.zip");
try (FileSystem fs = FileSystems.newFileSystem(path, null)) {
Files.walk(fs.getPath("/")).forEach(System.out::println);
}