Java 14 New Features
July 05, 2021
On this page we will discuss new features and enhancements introduced in Java 14.
Contents
Pattern Matching for instanceof
Operator (JEP 361)
This is a preview feature in Java 14. The instanceof
operator has been changed for pattern matching of an object that will make code more concise and robust.
Look at the below code.
if (animal instanceof Cow) { Cow a = (Cow) animal; System.out.println(a.getCowName()); }
instanceof
, we can write above code as following.
if (animal instanceof Cow a) { System.out.println(a.getCowName()); }
package com.concretepage; interface Animal { } class Cow implements Animal { String name; public Cow(String name) { this.name = name; } public String getCowName() { return name; } } class Horse implements Animal { String name; public Horse(String name) { this.name = name; } public String getHorseName() { return name; } } public class MyApp { public static void main(String[] args) { Animal ob = new Cow("Cow"); getName(ob); } static String getName(Animal animal) { if (animal instanceof Cow a) { System.out.println(a.getCowName()); } else if (animal instanceof Horse a) { System.out.println(a.getHorseName()); } return null; } }
Records (JEP 359)
This is a preview feature in Java 14. Therecord
is a new kind of type declaration like enum
. It is used for "plain data carriers" classes that contain data not meant to be altered and only the most fundamental methods such as constructors and accessors. Find the sample example.
package com.concretepage; record Rectangle(int length, int width) { } public class MyApp { public static void main(String[] args) { Rectangle rec = new Rectangle(12, 5); System.out.println(rec.length()); //12 System.out.println(rec.width()); //5 } }
Rectangle
.
record Rectangle(int length, int width) { }
final class Rectangle { final int length; final int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } int length() { return length; } int width() { return width; } }
1. All the members of
record
are final.
2. We have only accessors methods with same name as field names in a
record
.
Switch Expressions (JEP 361)
Switch Expressions was introduced in Java 12 as preview feature. Now this feature has been adopted as permanent in Java 14. Switch expressions are used as "case L ->" and eliminates the use ofbreak
keyword. The switch expression has been extended to produce value using yield
in Java 13.
Find the sample example.
package com.concretepage; public class MyApp { public static void main(String[] args) { int day = 2; String x = switch (day) { case 1 -> "Monday"; case 2 -> "Tuesday"; case 3 -> "Wednesday"; default -> "NA"; }; System.out.println(x); } }
Accounting Currency Format Support (JDK-8215181)
In some locales, accounting currency format supports parenthesis such as using "u-cf-account" unicode locale extension, the localeLocale.US
will format to "($20.50)" instead of "-$20.50".
DecimalFormat df1 = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.US); System.out.println(df1.format(-20.50)); //-$20.50 Locale myLocale = new Locale.Builder().setLocale(Locale.US) .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "cf-account").build(); DecimalFormat df2 = (DecimalFormat) NumberFormat.getCurrencyInstance(myLocale); System.out.println(df2.format(-20.50)); //($20.50)
Other New Features and Enhancements
1. JEP 359: A new classjava.lang.Record
has been introduced as preview feature. As we know that java.lang.*
is imported by default in our java class. If we create a class named Record
in our package such as com.cp
and when we write import com.cp.*
then our Record
class will not be imported. We need to import it explicitly as following.
import com.cp.Record
2. JDK-8164993: The following methods specification has been changed to specify that an
IllegalArgumentException
will be thrown if the buffer parameters are read-only.
DatagramChannel.receive() FileChannel.read(ByteBuffer,long) ReadableByteChannel.read() ScatteringByteChannel.read()
- XX:+UnlockExperimentalVMOptions -XX:+UseZGC
5. JEP 345: The G1 garbage collector now tries to allocate and keep objects on the same NUMA node in the young generation across garbage collections.
6. JEP 349: JDK Flight Recorder (JFR) now supports continuous monitoring of a Java application by allowing events to be consumed dynamically using a new API located in the
jdk.jfr.consumer
package.
7. JDK-8233228: Weak Named Curves in TLS, CertPath, and Signed JAR are disabled by default.
8. JDK-8231507: The Apache Santuario library has been upgraded to version 2.1.4. A new property has been added as following.
com.sun.org.apache.xml.internal.security.parser.pool-size
javac
plugins has been allowed to be invoked by default.
10. JDK-8230814: A new method
declaration
has been added to SAX ContentHandler
to receive notification of the XML declaration.
References
JDK 14 Release NotesJava Language Updates