Java 14 Features

Java 14 was released as a short-term (non-LTS) release, but it introduced some very important language improvements—especially around cleaner modeling of data and better control-flow expressions.

This post covers the most useful Java 14 features with simple examples you can try quickly.


1) Switch Expressions (Standard) ✅

In Java 14, switch expressions became a standard feature (not preview).
You can now use switch as an expression that returns a value.

Why it matters

  • Less boilerplate than break
  • Safer (exhaustive handling in many cases)
  • Cleaner assignment-style code

Example: Old switch vs Java 14 switch expression

// Before (classic switch statement)
String dayType;
switch (day) {
  case "SAT":
  case "SUN":
    dayType = "WEEKEND";
    break;
  default:
    dayType = "WEEKDAY";
}

// Java 14 (switch expression)
String dayType2 = switch (day) {
  case "SAT", "SUN" -> "WEEKEND";
  default -> "WEEKDAY";
};

Example: Using yield in a switch block

int fee = switch (plan) {
  case "BASIC" -> 99;
  case "PRO" -> 199;
  default -> {
    int defaultFee = 149;
    yield defaultFee; // yield returns a value from the block
  }
};

2) Records (Preview) 🧪

Java 14 introduced Records as a preview feature.
A record is a compact way to declare a class that is mainly data.

Why it matters

  • Eliminates boilerplate: constructors, getters, equals(), hashCode(), toString()
  • Makes DTOs / request models very clean

Example: DTO with record

public record Employee(String name, int age) { }

Usage:

Employee e = new Employee("Jiyanshi", 20);
System.out.println(e.name()); // getter-like access
System.out.println(e);        // Employee[name=Jiyanshi, age=20]

Note for running preview features

To compile/run records in Java 14:

javac --release 14 --enable-preview MyApp.java
java --enable-preview MyApp

3) Pattern Matching for instanceof (Preview) 🧪

Java 14 previewed pattern matching for instanceof, allowing you to test + cast in one step.

Before Java 14

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

Java 14 (Preview)

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

Cleaner and less error-prone.


4) Helpful NullPointerExceptions ✅

Java 14 improved NullPointerException messages to show which part was null.

Example scenario

System.out.println(user.getAddress().getCity().toUpperCase());

Earlier, you’d often just get a generic NPE.
In Java 14+, the message is more descriptive (for example, it may indicate user.getAddress() was null).

Why it matters

  • Faster debugging
  • Less time spent guessing which reference was null

5) Text Blocks (Second Preview) 🧪

Text blocks make multi-line strings readable (useful for JSON, SQL, HTML).

Example: JSON

String json = """
{
  "name": "Jiyanshi",
  "role": "Java Developer",
  "exp": 12
}
""";
System.out.println(json);

Example: SQL

String sql = """
SELECT id, name
FROM employee
WHERE age > 30
ORDER BY name
""";

6) New/Updated APIs (Useful Mentions)

Java 14 included several runtime and library improvements. Two practical mentions:

java.util.concurrent and performance updates

Many internal performance improvements happened across releases. You may not “see” them in code, but they help in production workloads.

Packaging tool jpackage (Incubator)

Java 14 included jpackage as an incubator tool to create platform installers (msi/pkg/deb). It matured further in later releases.


Java 14 Summary Table

  • Switch Expressions → Standard ✅
  • Records → Preview 🧪
  • Pattern Matching for instanceof → Preview 🧪
  • Helpful NPE messages → Standard ✅
  • Text Blocks → Preview (improved) 🧪
  • jpackage → Incubator 🧪

FAQ

Is Java 14 LTS?

No. Java 14 is not an LTS release. If you want LTS, consider Java 11, 17, or 21.

Should I use Java 14 in production?

Usually no, unless your org has a controlled upgrade policy.
But learning Java 14 features is still valuable because many became standard and are widely used now.