咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

JDK 14 新特性

2020年3月17日 JDK 14 发布,非 LTS 版本。

新特性: http://openjdk.java.net/projects/jdk/14/

  • 305: Pattern Matching for instanceof (Preview)
  • 343: Packaging Tool (Incubator)
  • 345: NUMA-Aware Memory Allocation for G1
  • 349: JFR Event Streaming
  • 352: Non-Volatile Mapped Byte Buffers
  • 358: Helpful NullPointerExceptions
  • 359: Records (Preview)
  • 361: Switch Expressions (Standard)
  • 362: Deprecate the Solaris and SPARC Ports
  • 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
  • 364: ZGC on macOS
  • 365: ZGC on Windows
  • 366: Deprecate the ParallelScavenge + SerialOld GC Combination
  • 367: Remove the Pack200 Tools and API
  • 368: Text Blocks (Second Preview)
  • 370: Foreign-Memory Access API (Incubator)

中文

  • 305: instanceof的模式匹配 (预览)
  • 343: 打包工具 (Incubator)
  • 345: G1的NUMA内存分配优化
  • 349: JFR事件流
  • 352: 非原子性的字节缓冲区映射
  • 358: 友好的空指针异常
  • 359: Records (预览)
  • 361: Switch表达式扩展 (标准)
  • 362: 弃用Solaris和SPARC端口
  • 363: 移除CMS(Concurrent Mark Sweep)垃圾收集器
  • 364: macOS系统上的ZGC
  • 365: Windows系统上的ZGC
  • 366: 弃用ParallelScavenge + SerialOld GC组合
  • 367: 移除Pack200 Tools和API
  • 368: 文本块 (第二个预览版)
  • 370: 外部存储器API (Incubator)
1
2
3
4
5
6
7
8
9
if (o instanceof String) {
String s = (String)o;
... use s ...
}

// New code
if (o instanceof String s) {
... use s ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// instanceof 太多
static String formatter(Object o) {
String formatted = "unknown";
if (o instanceof Integer i) {
formatted = String.format("int %d", i);
} else if (o instanceof Long l) {
formatted = String.format("long %d", l);
} else if (o instanceof Double d) {
formatted = String.format("double %f", d);
} else if (o instanceof String s) {
formatted = String.format("String %s", s);
}
return formatted;
}

// 用switch
static String formatterPatternSwitch(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}

参考