Java 14 New Features

Java SE 14 was released in March 2020.

The full list of new features in JDK 14 can be found here:

https://openjdk.org/projects/jdk/14/ 

In this tutorial, we will look over some of these new features and examine some of them.

1. JEP 305: Pattern Matching for instanceof (Preview)

With this JEP, instanceof now allows casting of the checked object to a variable.

Before Java 14:

if (obj instanceof String) {
    String s = (String) obj;
    // use s
}

With Java 14:

if (obj instanceof String s) {
    // can use s here
} else {
    // can't use s here
}

2. JEP 345: NUMA-Aware Memory Allocation for G1

This JEP improves G1 performance on large machines by implementing NUMA-aware(non-uniform memory access) memory allocation.

Use the +XX:+UseNUMA option to enable it.

3. JEP 358: Helpful NullPointerExceptions

This JEP improves the usability of NullPointerException generated by the JVM by describing precisely which variable was null.

Let's look over the following code which throws NPE.

String s = null;
int length = s.length();
System.out.println("length: " + length);

Normally the code produces the following error when it runs:

java.lang.NullPointerException
    at com.javalopment.JEP358HelpfulNullPointerExceptions.testNPEs(JEP358HelpfulNullPointerExceptions.java:11)

With this JEP, we can now execute the code with -XX:+ShowCodeDetailsInExceptionMessages flag and the error produced will be more descriptive by pointing which parameter is null.

java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
    at com.javalopment.JEP358HelpfulNullPointerExceptions.testNPEs(JEP358HelpfulNullPointerExceptions.java:11)

4. JEP 359: Records (Preview)

5. JEP 361: Switch Expressions (Standard)

The preview feature for returning value with switch expressions in Java 12/13 is now standard and enabled by default.

6. JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector

With Java 9, the JEP 291 deprecated the CMS collector for removal in order to accelerate the development of other collectors. And now it's time to remove CMS.

7. JEP 368: Text Blocks (Second Preview)


Yorumlar

Popular

Java EE Technology Stack

Dependency Injection In Java and CDI

Proxy Server