JDK 14 – The new features in Java 14
Recently, Java changed its release cycle more rapidly. JDK 13 released on September 2019 and JDK 14 is scheduled for a production release on 17 March 2020. The features targeted to JDK 14 as follows,
1. Pattern Matching for instanceof
Java instanceof operator is used to test whether the object is an instance of the specified type. With Java 14 instanceof operator enhance to the pattern matching as bellow,
Before Java 14;
// need to declare and cast again the object if (obj instanceof String) { String str = (String) obj; str.contains(..) }
With Java 14;
// no need to declare str object again with casting if (!(obj instanceof String str)) { str.contains(..); }
2. Helpful NullPointerExceptions
Referance – JEP 358: Helpful NullPointerExceptions
With java 14 improve the usability of NullPointerExceptions generated by the JVM by describing precisely which variable was null. It will print the The JVM will print out the method, filename, and line number that caused the NullPointerException.
Exception in thread "method" java.lang.NullPointerException at Filename.method(linenumber)
The NullPointerExceptions error message may contain variable names from the source code. This can expose the security risk.
3. Switch Expressions (Standard)
Switch can be used as either a statement or an expression, and so that both forms can use either traditional case … : labels (with fall through) or new case … -> labels, with a further new statement for yielding value from a switch expression. This was a preview language feature in JDK 12 and JDK 13 and become permanent in JDK 14.
Example
switch (day) { case MONDAY, FRIDAY, SUNDAY -> System.out.println(6); case TUESDAY -> System.out.println(7); case THURSDAY, SATURDAY -> System.out.println(8); case WEDNESDAY -> System.out.println(9); }
4. Non-Volatile Mapped Byte Buffers
JDK 14 include new JDK-specific file mapping modes so that the FileChannel API can be used to create MappedByteBuffer instances that refer to non-volatile memory.
This JEP makes use of two related enhancements :
1. Support implementation-defined Map Modes (JDK-8221397)
2.MappedByteBuffer::force method to specify range (JDK-8221696)
4. Flight Recorder Event Streaming ( JFR Event Streaming ) for Continuous Monitoring
This will provides an API for the continuous consumption of JFR data from both in-process and out-of-process applications. And record the same set of events as in the non-streaming case, with overhead less than 1% and streaming must be able to co-exist with non-streaming recordings, both disk and memory-based.
But there can be risks in API callbacks may provoke JFR events, which could lead to infinite recursion. This can be mitigated by not recording events in such a situation.
Java 13 extends the previous Java 12 Switch Expressions by adding a new yield keyword to return a value from switch expression and breaks syntax is no longer compiled in Java 13, it uses yield instead.
6. NUMA-Aware Memory Allocation for G1
This is a garbage collector, intended to improve G1 performance on large machines.
7. Packaging Tool (Incubator)
This is a tool for packaging self-contained Java applications into a platform-specific package that includes all of the necessary dependencies. The application may be provided as a collection of ordinary JAR files or as a collection of modules. The supported platform-specific package formats of Linux (deb and rpm ), macOS( pkg and dmg) and Windows (msi and exe).
Other features will be introduced in JDK 14 are Deprecate the Solaris and SPARC Ports, Remove the Concurrent Mark Sweep (CMS) Garbage Collector, ZGC on macOS, ZGC on Windows, Deprecate the ParallelScavenge + SerialOld GC Combination, Remove the Pack200 Tools and API, Text Blocks (Second Preview), Foreign-Memory Access API.
Related Articles