DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

  1. DZone
  2. Refcards
  3. Java 14
refcard 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.

Free PDF for Easy Reference
refcard cover

Written By

author avatar Simon Ritter
Deputy CTO, 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

  • JDK 14 Feature List 
  • JEP 359: Records 
  • JEP 305: Pattern Matching for instanceof 
  • JEP 370: Foreign-Memory Access API 
  • JDK 14 API Documentation 

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

Like This Refcard? Read More From DZone

related article thumbnail

DZone Article

A First Look at Records in Java 14
related article thumbnail

DZone Article

JDK 14: CMS GC Is OBE
related article thumbnail

DZone Article

Maximize Kubernetes Security: Automate TLS Certificate Management With Cert-Manager on KIND Clusters
related article thumbnail

DZone Article

Combatting the 3 AM Ransomware Menace
related refcard thumbnail

Free DZone Refcard

Introduction to Cloud-Native Java
related refcard thumbnail

Free DZone Refcard

Java 15
related refcard thumbnail

Free DZone Refcard

Java 14
related refcard thumbnail

Free DZone Refcard

Getting Started With Headless CMS

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

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

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}
by
DZone Core CORE
· {{ parent.articleDate | date:'MMM. dd, yyyy' }} {{ parent.linkDate | date:'MMM. dd, yyyy' }}
Tweet
{{ parent.views }} ViewsClicks
  • Edit
  • Delete
  • {{ parent.isLocked ? 'Enable' : 'Disable' }} comments
  • {{ parent.isLimited ? 'Remove comment limits' : 'Enable moderated comments' }}