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 |
Now in Java 14
if (obj instanceof String s) { // instanceof, cast and bind variable in one line. |
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; } public static String showUpperCase(String str){ } |
Before Java 14.
$ D:/jdk-14/bin/java Test Exception in thread "main" java.lang.NullPointerException |
Java 14 with -XX:+ShowCodeDetailsInExceptionMessages option
$ D:/jdk-14/bin/java -XX:+ShowCodeDetailsInExceptionMessages Test Exception in thread "main" java.lang.NullPointerException: |
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) { |