New Features in Java 14: Switch Expressions, Pattern Matching for instanceof, Helpful NullPointerExceptions, Records and More
The article introduces the major Java 14 enhancements—including standard switch expressions, preview pattern matching for instanceof, more informative NullPointerExceptions, preview records, text blocks, and several JVM and library improvements—while providing code examples that illustrate how each feature simplifies Java development.
Since the release of Java 13 in September 2019, new Java versions have followed a six‑month cadence, and the non‑LTS Java 14 was released on 17 March 2020.
The most notable Java 14 features are:
Standard switch expressions (JEP 361)
Pattern matching for instanceof (preview, JEP 305)
Helpful NullPointerException messages (JEP 358)
Records (preview, JEP 359)
Text blocks (preview 2, JEP 368)
Package tool (incubator, JEP 343)
NUMA‑aware G1 memory allocation (JEP 345)
JFR event streaming (JEP 349)
Non‑volatile mapped byte buffers (JEP 352)
ZGC on macOS (JEP 364) and Windows (JEP 365)
External memory access API (incubator, JEP 370)
Switch Expressions
After being previewed in Java 12 and Java 13, switch expressions became a standard feature in Java 14. They allow a more concise lambda‑style syntax, exhaustive case checking, and the use of yield to return a value from the expression.
String result = switch (day) {
case "M", "W", "F" -> "MWF";
case "T", "TH", "S" -> "TTS";
default -> {
if (day.isEmpty())
yield "Please insert a valid day.";
else
yield "Looks like a Sunday.";
}
};
System.out.println(result);Pattern Matching for instanceof
Previously, developers had to write an explicit cast after an instanceof check. Java 14 introduces pattern matching, allowing the cast to be combined with the type test.
// Before Java 14
if (obj instanceof Article) {
Article a = (Article) obj;
System.out.println(a.getAuthor());
}
// Java 14 and later
if (obj instanceof Article a) {
System.out.println(a.getAuthor());
}Helpful NullPointerException Messages
NullPointerExceptions have long been a pain point. Java 14 improves the JVM output to include the exact expression that was null, making debugging much easier.
// Traditional stack trace
Exception in thread "main" java.lang.NullPointerException
at NullPointerExample.main(NullPointerExample.java:5)
// Java 14 enhanced message
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Blog.getAuthor()" because the return value of "Article.getBlog()" is null
at NullPointerExample.main(NullPointerExample.java:4)Records (preview)
Records provide a compact syntax for data‑carrier classes. The compiler automatically generates constructors, accessors, equals , hashCode , and toString methods, reducing boilerplate similar to Kotlin data classes.
record Author() {}
// or
record Author(String name, String topic) {}These enhancements, together with other improvements such as text blocks, package tool, and ZGC support on macOS and Windows, make Java 14 a significant step forward for developers.
High Availability Architecture
Official account for High Availability Architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.