Curious Minds

Clear answers to science and history questions

Java 14: Features, Records, and What Changed

· 6 min read · Science

Java 14 is special because it introduced records and pattern matching for instanceof as preview features, and it made switch expressions a permanent part of the language. Released in March 2020, it also brought helpful NullPointerException messages, a packaging tool, and several JVM improvements that made it a notable step forward for developer productivity.

Background: Where Java 14 Fits in the Release Cycle

Java 14 arrived on March 17, 2020, as a non-LTS (Long-Term Support) release. It followed Java 13, which had already previewed switch expressions and text blocks. Java 14 continued the six-month cadence that Oracle established with Java 9, delivering incremental improvements rather than a massive overhaul. Because it is not an LTS version, many production teams skipped it in favor of Java 11 or waited for Java 17, but it still served as a testing ground for features that later became standard.

The release included 16 JEPs (JDK Enhancement Proposals), covering language changes, library additions, and tooling. Some were preview features, meaning they were fully specified but not yet permanent and could change in future releases. Others were final and production-ready.

Key Language Features: Records and Pattern Matching

The most talked-about addition in Java 14 was records (JEP 359), introduced as a preview feature. A record is a compact way to declare a class that is a transparent carrier for immutable data. Instead of writing a full class with private final fields, a constructor, getters, and overrides for equals(), hashCode(), and toString(), you can write a single line:

public record Point(int x, int y) { }

This automatically generates the constructor, accessor methods x() and y(), and the standard methods. Records reduce boilerplate significantly and make intent clearer. They are especially useful for DTOs, multiple return values, and pattern matching.

Also previewed in Java 14 was pattern matching for instanceof (JEP 305). This allows you to declare a variable within an instanceof check and use it directly, eliminating the need for an explicit cast:

if (obj instanceof String s) {
    System.out.println(s.length());
}

This small change reduces verbosity and the risk of casting errors. Both records and pattern matching for instanceof became permanent in Java 16.

Switch Expressions Become Standard

Switch expressions were previewed in Java 12 and 13, and Java 14 made them final (JEP 361). The new syntax uses arrow labels and can return a value, making switch statements more concise and less error-prone. For example:

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    default -> throw new IllegalStateException("Invalid day: " + day);
};

This eliminates fall-through bugs and allows switch to be used as an expression. The old colon syntax is still supported for backward compatibility.

Helpful NullPointerExceptions

Java 14 introduced JEP 358, which makes NullPointerException messages more informative. Previously, a null pointer exception would only tell you that a null reference was encountered, often without pointing to the exact variable. With this change, the JVM can describe precisely which variable was null, using a detailed message. For example, instead of a generic message, you might see: Cannot invoke "String.length()" because "str" is null. This feature is enabled by default and greatly speeds up debugging.

Tooling and JVM Improvements

Java 14 included several other enhancements:

  • JEP 343: Packaging Tool (Incubator) – introduced jpackage, a tool for creating native installers for Java applications, reducing the need for third-party tools.
  • JEP 345: NUMA-Aware Memory Allocation for G1 – improved performance on large machines with non-uniform memory access.
  • JEP 349: JFR Event Streaming – allowed Java Flight Recorder data to be consumed continuously, useful for monitoring.
  • JEP 352: Non-Volatile Mapped Byte Buffers – enabled access to persistent memory.
  • JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector – CMS was deprecated in Java 9 and removed in Java 14.
  • JEP 365: ZGC on macOS and JEP 364: ZGC on Windows – extended the Z Garbage Collector to more platforms.
  • JEP 367: Remove the Pack200 Tools and API – Pack200 was removed.
  • JEP 370: Foreign-Memory Access API (Incubator) – allowed Java code to safely access memory outside the heap.

These changes made Java 14 a significant release for performance, tooling, and future directions.

Common Problems and How to Handle Them

Because Java 14 is not an LTS release, it received only six months of official updates. If you use it in production, you may encounter security patches ending quickly. Many teams use non-LTS releases for development and testing but deploy on LTS versions. Another common issue is that preview features require the --enable-preview flag at both compile and runtime, and they may change in later releases. Code using records or pattern matching from Java 14 might need adjustments when moving to Java 16 or later. Also, the removal of CMS and Pack200 could break older applications that rely on them; migration to G1 or another collector is necessary.

How Java 14 Compares to Other Releases

Compared to Java 11 (LTS), Java 14 offers records, pattern matching, switch expressions, and helpful NPE messages, but lacks long-term support. Compared to Java 17 (LTS), Java 14 is missing sealed classes, a finalized pattern matching for switch, and many other improvements. Java 14 is best seen as a stepping stone: it introduced features that matured in Java 16 and 17. For teams that want to experiment with modern Java syntax without committing to an LTS, Java 14 is a reasonable choice, but for production, Java 17 is generally preferred.

Frequently Asked Questions

Is Java 14 still supported?

No. Oracle stopped providing updates for Java 14 in September 2020, six months after release. It is not an LTS version, so it does not receive long-term security patches. For production use, an LTS release like Java 11 or Java 17 is recommended.

What is the difference between a record and a regular class?

A record is a concise way to declare an immutable data carrier. It automatically generates a canonical constructor, accessor methods, and implementations of equals(), hashCode(), and toString(). A regular class requires you to write these explicitly and allows mutable fields and additional behavior.

Can I use records in Java 14 for production code?

Records in Java 14 are a preview feature, so they are not recommended for production without careful consideration. They require the --enable-preview flag and may change in future releases. They became permanent in Java 16.

Why did Java 14 remove the CMS garbage collector?

CMS was deprecated in Java 9 because it was difficult to maintain and had performance limitations. Java 14 removed it entirely to reduce maintenance burden. Users are encouraged to migrate to G1 or other collectors like ZGC or Shenandoah.

What are helpful NullPointerExceptions?

This feature, enabled by default in Java 14, provides more detailed messages when a NullPointerException occurs, indicating exactly which variable was null. It helps developers diagnose issues faster without extra logging.

Is Java 14 faster than Java 11?

Java 14 includes performance improvements such as NUMA-aware memory allocation for G1 and JFR event streaming, but overall speed gains depend on the workload. For many applications, the difference is modest. The main benefits are developer productivity features rather than raw performance.

In this article
  1. Background: Where Java 14 Fits in the Release Cycle
  2. Key Language Features: Records and Pattern Matching
  3. Switch Expressions Become Standard
  4. Helpful NullPointerExceptions
  5. Tooling and JVM Improvements
  6. Common Problems and How to Handle Them
  7. How Java 14 Compares to Other Releases
  8. Frequently Asked Questions