What is new in Java 14
1. Introduction
In this article, we will take a look at the new features of Java 14. Java 14 release consisted of features from 16 different JDK enhancement proposals (JEPs).
2. Java 14
We look at the following features in Java 14
- Switch Expressions
- Helpful Null Pointer Exception Handling
- Numa (Non-Uniform Memory Access) Aware Memory Allocation
- JFR (JDK Flight Recorder) Event Streaming
- Preview Features
- Records
- Text Blocks
- Pattern Matching for Instance Of
- Incubator Features
- Non-volatile Mapped Byte Buffer
- Packaging Tool
- Foreign Memory access API
2.1 Prerequisites
Java 14 is required on the Linux, Windows, or Mac operating systems. Eclipse Oxygen can be used for this example.
2.2 Download
You can download Java 14 from the Oracle web site. Eclipse Oxygen can be downloaded from the Eclipse web site.
2.3 Setup
2.3.1 Java Setup
Below are the setup commands required for the Java Environment.
Setup
JAVA_HOME="/desktop/jdk14" export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
2.4 IDE
2.4.1 Eclipse Oxygen Setup
The ‘eclipse-java-oxygen-2-macosx-cocoa-x86_64.tar’ can be downloaded from the eclipse website. The tar file is opened by double click. The tar file is unzipped by using the archive utility. After unzipping, you will find the eclipse icon in the folder. You can move the eclipse icon from the folder to applications by dragging the icon.
2.4.2 Launching IDE
Eclipse has features related to language support, customization, and extension. You can click on the Eclipse icon to launch Eclipse. The Eclipse screen pops up as shown in the screenshot below:
You can select the workspace from the screen which pops up. The attached image shows how it can be selected.
You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.
Java Hello World
class prints the greetings. The screenshot below is added to show the class and execution on the eclipse.
2.5 Java 14 New Features
Let us look in detail, the new features of Java 14 which were released in March 2020. This release is not a long-term support release. It is a feature release having security updates. the release after Java 14 will be Java 15. Java 15 is due in September 2020. The next release of Java will be six months from the current release.
2.5.1 Switch Expressions
switch
Expression are now a feature in Java 14. It was in preview mode for Java 12 and 13. switch
expression has yield statements to return the output from the expression.
Let us look at the example where switch
expression is used:
Switch Expression
public class SwitchExample { public static void main(String[] args) { String week_day = "Wed"; String output = switch (week_day) { case "Mon" -> "Monday"; case "Tue" -> "Tuesday"; case "Wed" -> "Wednesday"; case "Thu" -> "Thursday"; case "Fri" -> "Friday"; case "Sat" -> "Saturday"; default -> { if(week_day.isEmpty()) yield "not a valid day."; else yield "it is a sunday."; } }; System.out.println(output); } }
To compile and execute the code above, the following commands are used:
Run Command
/usr/libexec/java_home -v 14 --exec javac SwitchExample.java /usr/libexec/java_home -v 14 --exec java SwitchExample
The output of the above commands will be as below:
Output
Wednesday
2.5.2 Helpful Null Pointer Exception Handling
The exception stack trace is changed to show the exception details. This is a runtime feature added in Java 14.
Let us look at the example below:
Null Pointer Example
public class Customer { private String name; public String getName() { return this.name; } public Customer getInstance() { return null; } public static void main(String[] args) { Customer customer = new Customer(); String result = customer.getInstance().getName(); } }
To compile and execute the code above, the following commands are used:
Run Commands
/usr/libexec/java_home -v 14 --exec javac Customer.java /usr/libexec/java_home -v 14 --exec java Customer
The output of the above commands will be as below:
Output
Exception in thread "main" java.lang.NullPointerException at Customer.main(Customer.java:17)
2.5.3 Numa Aware Memory Allocation
NUMA is a term for Non-Uniform Memory Access. It is a method of creating a cluster of processors in a multiprocessing environment. In this environment, memory is shared and performance is higher than the normal processing environment. Java 14 has a feature related to NUMA aware Memory Allocation. This helps in managing the G1 garbage collection process and improves performance. A heap is a group of fixed size regions marked for G1 garbage collection. A region is a group of physical pages. If the physical page is large, it stands for a single region. Java 14 has UseNUMA option which can be used with java commands as +XX:+UseNUMA
Java 14 has removed CMS (Concurrent Mark Sweep) garbage collection. CMS options are no longer there in this release.
2.5.4 JFR Event Streaming
Java 14 has API for JDK Flight Recorder data consumption. JDK Flight Recorder helps in profiling the java applications in runtime. The events are recorded during runtime profiling. This helps in monitoring the application in runtime. Previously the recording file needs to be read for analyzing the events. Now the events are streamed during runtime profiling.
2.5.5 Preview Features
Let us look at the preview Features in Java 14. The preview features in Java 14 are Records, Pattern matching for the instance of operator and text blocks.
Records are classes that carry data which have immutable data. They hold data that cannot be modified.
Record Example
public class RecordExample { record Book(String name) { public String name() { return "Book " + name; } public Book{ } } public static void main(String[] args) { var book = new Book("Encyrption"); System.out.println(book.getClass().isRecord()); System.out.println(book.getClass().getRecordComponents()); } }
Pattern Matching for instanceof
the operator is another preview feature in Java 14. It helps in making the code type-safe and conditional components separated from objects. instanceof
operator helps in verifying the type of the given object reference. This operator returns true if the object is an instanceof
the specified class otherwise false. A code sample is shown below.
InstanceOf Example
public class InstanceOfExample { public String getName() { return "check"; } public static void main(String[] args) { Object example = new InstanceOfExample(); if (example instanceof InstanceOfExample instance) { System.out.println(instance.getName()); } } }
Text blocks are another preview feature in Java 14. A text block is a multi-line string literal which is used to create a string out of multiple lines. This helps in avoiding the escape sequences.
TextBlocks Example
public class TextBlockExample { public static void main(String[] args) { String mulitpleEscape = """ line1 \ line2 \ line3\ line4\ """; String multipleLine = """ line11 line21 \s line31 """; String multipleLineString = "line31\nline32 \nline33\n"; } }
2.5.6 Incubator Features
Let us look at Incubator Features in Java 14 which are Non-Volatile Memory Buffer, JPackage Tool, and Foreign Memory Access API. Java 14 has Non-volatile memory Mapped Byte Buffer to handle non-volatile storage. Non Volatile memory is related to Flash memory, Read-Only Memory, and other storage devices related. When the power is off, this memory is retained.
In Java 14, JPackage Tool is used for bundling Java applications into a package. This package will have JAR files. Java 14 has an incubator feature to handle foreign memory access from MapDB, Memcached, Ignite, and Netty’s ByteBuf API. API has three new components in memory handling such as Memory Segment, Memory Address, and Memory Layout.
3. Download the Source Code
You can download the full source code of this example here: What is new in Java 14