• Join StackChief
  • Blog
  • Tutorials
  • Questions
  • React
  • JavaScript
  • MongoDB
  • NodeJs
  • Kafka
  • Java
  • Spring Boot
  • Examples

Blog

Java 14 | New Features

Understanding the evolution of Java versions can be a bit daunting. If you're unsure, check out Which Version of Java Should You Use?.


As of this writing, Java has progressed well beyond version 14, so let's take a retrospective look at Java 14's features and how they've paved the way for newer iterations. While Java 14 wasn't a Long-Term Support (LTS) release, it introduced several exciting features that set the stage for future enhancements.

Key Takeaways

  • Pattern matching simplifies instance checks and typecasts.
  • Switch expressions enhance the traditional switch statements with more flexibility.
  • Records offer a concise way to create immutable data carriers.
  • Text blocks streamline the handling of multi-line strings.
  • Java 14 included a variety of experimental features that have evolved in later versions.

Pattern Matching for instanceof

Pattern matching for the instanceof operator reduces verbosity by combining a check and assignment in one step.

if (obj instanceof String) {
   String s = (String) obj;
   // use s
}

Becomes:

if (obj instanceof String s) {
   // can use s here
} else {
   // can't use s here
}

This syntactic sugar simplifies your code and lessens boilerplate.

Switch Expressions

With Java 14, switch expressions became a permanent feature, allowing both statements and expressions, bringing ease of use and new functionality.

switch (day) {
   case MONDAY              -> System.out.println("Monday");
   case TUESDAY, WEDNESDAY  -> System.out.println("Hump days");
   case THURSDAY            -> System.out.println("Almost Friday");
   case FRIDAY              -> System.out.println("Friday");
   case SATURDAY, SUNDAY    -> System.out.println("The weekend");
}

As an expression:

int numDays = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
};

This makes the switch construct more powerful and expressive.

Records

Records reduce boilerplate code needed for data-holding classes by automatically generating methods like equals(), hashCode(), and toString().

record Person(String first, String last) { }

This is equivalent to a full-fledged class with all necessary methods implemented.

Text Blocks

Text blocks introduced with Java 14 make multi-line string literals easier to manage.

String html = """
              <html>
                  <body>
                      <p>My HTML text</p>
                  </body>
              </html>
              """;

This improves readability and reduces the hassle of dealing with escaped newline characters.

NullPointerExceptions Enhancements

The addition of helpful NullPointerException messages in Java 14 was a game-changer for debugging.

a.b.c.d = 41
Exception in thread "main" java.lang.NullPointerException:
    Cannot read field "c" because "a.b" is null
at App.main(App.java:45)

This clarity in error messages helps quickly identify and resolve the source of the issue.

Packaging Tool (jpackage)

Java 14 included the jpackage tool as an incubator feature, allowing developers to package Java applications into native formats like .exe for Windows and .dmg for Mac.

Garbage Collection Improvements

Java 14 featured NUMA-aware memory allocation enhancements for the G1 garbage collector, optimizing performance on NUMA architectures.

JFR Event Streaming

With event streaming in Java Flight Recorder, developers could stream events in real-time without saving them to disk, thus cutting down on I/O overhead.

Expanded Support for ZGC

The experimental support for the Z Garbage Collector (ZGC) on macOS and Windows in Java 14 offered increased flexibility for applications requiring low latency garbage collection.

Conclusion

Java 14 was a significant milestone, introducing features that have since become standard in the language. These advancements reduced verbosity and improved performance, setting the stage for future Java releases. While experimenting with Java 14 was insightful at the time, newer versions continued to build on its innovations.

FAQ

What was the most significant feature in Java 14?

Many developers found pattern matching and records to be particularly influential, as they reduced common boilerplate and enhanced Java's syntax.

Is Java 14 still supported?

No, Java 14 is not a Long-Term Support (LTS) version. It's advisable to use the latest LTS version for stability and support.

Is ZGC stable on macOS and Windows?

Initially experimental in Java 14, ZGC has matured with subsequent releases, offering greater stability and support across platforms.

How do text blocks improve code readability?

Text blocks allow for clean, multi-line string declarations without the need for explicit newline characters, significantly improving the readability of code involving lengthy strings.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews
Comment