Java 14 Features

Download Java 14 here

  • Pattern Matching for instanceof (Preview) 
  • Packaging Tool (Incubator)
  • NUMA-Aware Memory Allocation for G1
  • JFR Event Streaming
  • Non-Volatile Mapped Byte Buffers
  • Helpful NullPointerExceptions
  • Records (Preview) (developer feature)
  • Switch Expressions (Standard) (developer feature)
  • 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) (developer feature)
  • Foreign-Memory Access API (Incubator) (developer feature)

 

Pattern Matching for instanceof

Before Java 14, we were used 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("java".equalsIgnoreCase(s)){
      
  }
}else {
       System.out.println("not a string");
}

 

Now in Java 14

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

 

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

 

Helpful NullPointerExceptions

Improved the description of NullPointerExceptions by telling which variable was null. Add -XX:+ShowCodeDetailsInExceptionMessages option to enable this feature.

A Java file that throws an NullPointerException

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.

$ D:/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

$ D:/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)

 

Switch Expressions (Standard)

The switch expression was a preview feature in Java 12 and Java 13; now it is officially JDK standard feature, it means from Java 14 and onward, we can return value via switch expressions without specifying the --enable-preview option.

We can use yield to return a value from a switch

private static int getValueViaYield(String mode) {
        int result = switch (mode) {
            case "a", "b":
                yield 1;
            case "c":
                yield 2;
            case "d", "e", "f":
                            // do something here...
                             System.out.println("Supports multi line block!");
                yield 3;
            default:
                yield -1;
        };
        return result;
    }