{{announcement.body}}
{{announcement.title}}
Refcard #330

Java 14

JDK 14 does not disappoint. In the latest JDK update, we see the usual API changes and low-level JVM enhancements, as well as exciting new language preview features and simplified debugging with NPE.

Published: Apr. 24, 2020
2,910
Free PDF for easy Reference

Brought to you by

Azul Systems
refcard cover

Written by

author avatar Simon Ritter Deputy CTO, Azul Systems
asset cover
Refcard #330

Java 14

JDK 14 does not disappoint. In the latest JDK update, we see the usual API changes and low-level JVM enhancements, as well as exciting new language preview features and simplified debugging with NPE.

Published: Apr. 24, 2020
2,910
Free PDF for easy Reference

Written by

author avatar Simon Ritter Deputy CTO, Azul Systems

Brought to you by

Azul Systems
Table of Contents

Introduction

Records

Pattern Matching Instanceof 

Helpful NullPointerException 

New APIs

JVM Changes

Other Features

Additional Resources

Section 1

Introduction

The release of JDK 14 resumes the six-month release cadence of OpenJDK and signifies that this is working very well.  Although some releases have been a little light on new features (JDK 13, for example), JDK 14 includes multiple exciting new features for developers.

In addition to the usual API changes and low-level JVM enhancements, JDK 14 also includes two language preview features in the form of records and pattern matching for instanceof.  All Java developers are familiar with NullPointerExceptions, and now, thankfully, they have been improved to simplify debugging. 

Java has always protected developers from the kind of common errors that occur in languages like C and C++ through the use of explicit pointers that can be manipulated and point to the wrong place.  Sometimes, this can be useful, and developers have resorted to using undocumented and unsupported APIs like sun.misc.Unsafe. In JDK 14, there is a new incubator module providing a foreign-memory access API.


This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 2

Records

Java is an object-oriented language; you create classes to hold data and use encapsulation to control how that data is accessed and modified. The use of objects makes manipulating complex data types simple and straightforward. 

The downside (until now) is that creating a data type has been verbose, requiring a lot of code even for the most straightforward cases. Let's look at the code needed for a basic two-dimensional point:

Java
 




x
33


1
public class Point {
2
  private final double x;
3
  private final double y;
4
 
5
  public Point(double x, double y) {
6
    this.x = x;
7
    this.y = y;
8
  }
9

          
10
  public double getX() {
11
    return x;
12
  }
12
  }
13

          
14
  public double getY() {
15
    return y;
16
  }
17
}



This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 3

Pattern Matching Instanceof 

In some situations, you do not know the exact type of an object. To handle this, Java has the instanceof operator that can be used to test against different types.  The drawback to this is that, having determined the type of an object, you must use an explicit cast if you want to use it as that type:

Java
 




xxxxxxxxxx
1


 
1
if (o instanceof String) {
2
  String s = (String)o;
3
  System.out.println(s.length);
4
}


 

In JDK 14, the instanceof operator has been extended to allow a variable name to be specified in addition to the type (this is also a preview feature). That variable can then be used without the explicit cast:

Java
 




xxxxxxxxxx
1


1
if (o instanceof String s)
2
  System.out.println(s.length);



This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 4

Helpful NullPointerException 

Anyone who's written more than a few lines of Java code will have experienced a NullPointerException at some point. Failing to initialize an object reference (or mistakenly explicitly setting it to null) and then trying to use the reference will cause this exception to be thrown.

In simple cases, finding the cause of the problem is straightforward. If we try and run code like this:

Java
 




xxxxxxxxxx
1
13
9


1
public class NullTest {
2
  List<String> list;
3

          
4
  public NullTest() {
5
    list.add("foo");
6
  }
7
}


 

The error generated is:

Java
 




xxxxxxxxxx
1


 
1
Exception in thread "main" java.lang.NullPointerException
2
     at jdk14.NullTest.<init>(NullTest.java:16)
3
     at jdk14.Main.main(Main.java:15)



This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above

Section 5

New APIs

There are 69 new API elements in the core class libraries of JDK 14. Here are the highlights:

java.io 

There is a new annotation type, Serial. This is intended to be used for compiler checks on Serialization. Specifically, annotations of this type should be applied to serialization-related methods and fields in classes declared to be Serializable. (It is similar in some ways to the Override annotation).

java.lang 

The Class class has two methods for the new Record feature, isRecord() and  getRecordComponents().  The getRecordComponents() method returns an array of  RecordComponent objects.  RecordComponent is a new class in the java.lang.reflect package with eleven methods for retrieving things, such as the details of annotations and the generic type of each component in the record.

Record is a new class that is the implicit supertype of all records and overrides the equals,  hashCode, and toString methods of Object.


This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above

Section 6

JVM Changes

In JDK 14, there are no functional changes to the JVM, but it does include JEPs that affect non-functional parts of the JVM.

  • JEP 345: NUMA-Aware Memory Allocation for G1 — This improves performance on large machines that use non-uniform memory architecture (NUMA).
  • JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector — Since JDK 9, G1 has been the default collector and is considered by most to be a superior collector to the CMS. Given the resources required to maintain two similar profile collectors, Oracle decided to deprecate CMS (also in JDK 9), and now, it has been deleted.
  • JEP 349: JFR Event Streaming — This allows more real-time monitoring of a JVM by enabling tools to subscribe asynchronously to Java Flight Recorder events.
  • JEP 364: ZGC on macOS and JEP 365: ZGC on Windows — ZGC is an experimental low-latency collector that was initially only supported on Linux. This has now been extended to the macOS and Windows operating systems.
  • JEP 366: Deprecate the ParallelScavenge and SerialOld GC combination — Oracle states that very few people use this combination and the maintenance overhead is considerable. Expect this combination to be removed at some point in the not-too-distant future.

This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above

Section 7

Other Features

There are a number of JEPs that relate to different parts of the OpenJDK: 

  • JEP 343: Packaging Tool — This is a simple packaging tool, based on the JavaFX  javapackager tool that was removed from the Oracle JDK in JDK 11. This is also an incubator feature. 
  • JEP 352: Non-volatile Mapped Byte Buffers — This adds new JDK-specific file mapping mode so that the FileChannel API can be used to create MappedByteBuffer instances that refer to non-volatile memory. A new module, jdk.nio.mapmode, has been added to allow MapMode to be set to  READ_ONLY_SYNC or WRITE_ONLY_SYNC.
  • JEP 361: Switch Expressions Standard — Switch expressions were the first preview feature added to OpenJDK in JDK 12. In JDK 13, feedback resulted in the break value syntax being changed to yield value. In JDK 14, switch expressions are no longer a preview feature and have been included in the Java SE standard.
  • JEP 362: Deprecate the Solaris and SPARC ports — Since Oracle is no longer developing either the Solaris operating system or SPARC chip architecture, they do not want to have to continue the maintenance of these ports. This might be picked up by others in the Java community.
  • JEP 367: Remove the Pack 200 Tools and API — Another feature that was deprecated in JDK 11 and now removed from JDK 14.  The primary use of this compression format was for jar files used by applets. Given the browser plugin was removed from Oracle JDK 11, this seems a reasonable idea.
  • JEP 368: Text Blocks — These were included in JDK 13 as a preview feature, and they continue with a second iteration (still in preview) in JDK 14.  Text blocks provide support for multi-line string literals. The changes are the addition of two new escape sequences. The first suppresses the inclusion of a newline character by putting a \ at the end of the line (this is common in many other places in software development). The second is \s, which represents a single space. This can be useful to prevent the stripping of whitespace from the end of a line within a text block.

As you can see, JDK 14 has packed in a lot of new features that will make the lives of developers much easier.


This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above.

Section 8

Additional Resources


This is a preview of the Java 14 Refcard. To read the entire Refcard, please download the PDF from the link above.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}