Java 14的新增功能
作者:互联网
Java 14 reached General Availability on 17 March 2020, download Java 14 here.
Java 14 features.
- JEP 305: Pattern Matching for instanceof (Preview) (developer feature)
- JEP 343: Packaging Tool (Incubator)
- JEP 345: NUMA-Aware Memory Allocation for G1
- JEP 349: JFR Event Streaming
- JEP 352: Non-Volatile Mapped Byte Buffers
- JEP 358: Helpful NullPointerExceptions
- JEP 359: Records (Preview) (developer feature)
- JEP 361: Switch Expressions (Standard) (developer feature)
- JEP 362: Deprecate the Solaris and SPARC Ports
- JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
- JEP 364: ZGC on macOS
- JEP 365: ZGC on Windows
- JEP 366: Deprecate the ParallelScavenge + SerialOld GC Combination
- JEP 367: Remove the Pack200 Tools and API
- JEP 368: Text Blocks (Second Preview) (developer feature)
- JEP 370: Foreign-Memory Access API (Incubator) (developer feature)
JEP 305: Pattern Matching for instanceof (Preview)
Before Java 14, we use instanceof-and-cast
to check the object’s type and cast to a variable.
if (obj instanceof String) { // instanceof
String s = (String) obj; // cast
if("jdk14".equalsIgnoreCase(s)){
//...
}
}else {
System.out.println("not a string");
}
Now Java 14, we can refactor above code like this:
if (obj instanceof String s) { // instanceof, cast and bind variable in one line.
if("jdk4".equalsIgnoreCase(s)){
//...
}
}else {
System.out.println("not a string");
}
if obj
is an instance of String
, then it is cast to String
and assigned to the binding variable s
.
JEP 343: Packaging Tool (Incubator)
New jpackage
tool to package a Java application into a platform-specific package.
- Linux: deb and rpm
- macOS: pkg and dmg
- Windows: msi and exe
For example, package the JAR
file into an exe
file on Windows.
P.S Update jpackage
example here
JEP 345: NUMA-Aware Memory Allocation for G1
New NUMA-aware memory allocation mode, improves the G1 performance on large machines. Add +XX:+UseNUMA
option to enable it.
JEP 349: JFR Event Streaming
Improved the existing JFR to support event streaming, it means now we can stream the JFR events in real-time, without the need to dump the recorded events to disk and parse it manually.
The JDK Flight Recorder (JFR) is a tool for collecting diagnostic and profiling data about a running Java application. Normally, we start a recording, stop it, dump the recorded events to disk for parsing, it works well for profiling, analysis, or debugging.
Related JEP 328: Flight Recorder
P.S Update JFR Event Streaming example here_
JEP 352: Non-Volatile Mapped Byte Buffers
Improved FileChannel
API to create MappedByteBuffer
that access to non-volatile memory (NVM) – a memory that can retrieve stored data even after having been power cycled. For example, this feature ensures that any changes which might still be in the cache are written back to memory.
P.S Only Linux/x64 and Linux/AArch64 OS support this!
JEP 358: Helpful NullPointerExceptions
Improved the description of NullPointerExceptions
by telling which variable was null. Add -XX:+ShowCodeDetailsInExceptionMessages
option to enable this feature.
A simple Java file that throws an NullPointerException
.
Test.java
import java.util.Locale;
public class Test {
public static void main(String[] args) {
String input = null;
String result = showUpperCase(input); // NullPointerException
System.out.println(result);
}
public static String showUpperCase(String str){
return str.toUpperCase(Locale.US);
}
}
Before Java 14.
$ /usr/lib/jvm/jdk-14/bin/java Test
Exception in thread "main" java.lang.NullPointerException
at Test.showUpperCase(Test.java:15)
at Test.main(Test.java:9)
Java 14 with -XX:+ShowCodeDetailsInExceptionMessages
option.
$ /usr/lib/jvm/jdk-14/bin/java -XX:+ShowCodeDetailsInExceptionMessages Test
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.toUpperCase(java.util.Locale)" because "<parameter1>" is null
at Test.showUpperCase(Test.java:15)
at Test.main(Test.java:9)
P.S Please enable this feature by default 标签:JEP,Java,14,新增,println,Test,java
来源: https://www.cnblogs.com/zhaokejin/p/15607894.html