Java 14 | New Features

Versioning in Java can be kind of confusing. Be sure to check out Which Version of Java Should You Use?.


Java 14 is the latest stable version of Java SE. While it doesn't feature long term support (LTS), it includes some cool new features worth exploring...

Pattern Matching (preview)

Pattern matching reduces the verbosity of Java...

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

vs

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

With pattern matching, the instanceof operator both checks that a value has a certain shape AND assigns it to a binding variable with the proper type.

Switch Expressions

switch statements were available in 12,13 as a preview feature. They are now a standard feature meaning they are enabled by default.

Java 14 allows you to use switch expressions as either expressions:

 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");
}

or statements:

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

Records (preview)

Records are like syntactic sugar for declaring classes...

record Person(String first, String last) { }

This is a shortcut for...

final class Person {
    public final String first;
    public final String last;

    public Person(String first, String last) {
        this.first = first;
        this.last = last;
    }
    //equals, hashCode, toString

Records make it easier to "model data as data" by removing the boilerplate code needed to create a traditional data carrier class.

Using records eliminates the need to define equals(), hashCode(), and toString() methods. Instead, these are implemented automatically by using record.

Records automatically define a public constructor with the same signature as the state description as well as a private final field for each component of the state description (first, last in this example).

Text Blocks (preview)

Text blocks make it easier to work with multi-line strings in Java...

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

vs

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

Text blocks were a preview feature of Java 13. They are now considered a "second preview" in Java 14.

Better NPE

Java 14 features more helpful NullPointerExceptions....

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 is more user friendly and can easily pinpoint where the NullPointerException is occurring.

Packaging Tool (incubator)

Using a new packaging tool, you can package .JAR files as native output like .exe for Windows and .dmg for Mac.

Garbage Collection Improvements

Java 14 features NUMA-Aware memory allocation for G1.

JFR Event Streaming

Java Flight Recorder now supports streaming events. This allows you to to stream events from JFR without writing them to disk.

This cuts i/o overhead and allows for the continuous consumption of JFR data.

ZGC on Mac / Windows (experimental)

Applications which server millions of users concurrently require lots of memory. ZGC is a newer garbage collector tailored specifically to this problem.

ZGC is typically run on Linux, but Java 14 is making that possible for Windows and Mac.

Conclusion

Java 14 adds some promising new features. Many improvements to reduce verbosity and improved garbage collection are certainly reasons to experiment with Java 14. Remember that LTS will not be offered and that many of these features are preview based or experimental.

Play around with Java 14 but don't use it in production.

Your thoughts?