What's new in Java14

Introduction

On March 17. 2020 Java JDK14 reached General Availability. In this post I want share a quick look at 3 interesting features with you.

1. JEP 361: Switch Expressions

The first new feature I want to share with you is switch expressions.
First an example using the well known Java switch syntax:

enum Day { Monday, Tuesday, Wednesday, Thursday, Firday, Saturday, Sunday }

Day day = Day.Monday;
int letters = 0;
switch (day) {
    case Monday:
    case Firday:
    case Sunday:
        letters = 6;
        break;
    case Tuesday:
        letters = 7;
        break;
    case Thursday:
    case Saturday:
        letters = 8;
        break;
    case Wednesday:
        letters = 9;
        break;
    default:
        letters = -1;
        break;
}

If you look at the new switch statement its much more elegant and simple.

var key = switch (type) {
    case KUNDE -> "KUNDE_KEY";
    case VERMITTLER, VERSICHERUNG -> "DEFAULT_KEY";
    default -> "..."
};

2. JEP 358: Helpful NullPointerExceptions

Who’s not asking himself where this NullPointerException came from. The JEP 358 has the goal to give us a answer to this question.

if a.i = b.j; throws an NPE:
Exception in thread "main" java.lang.NullPointerException:
        Cannot read field "j" because "b" is null
    at Prog.main(Prog.java:5)

Of course, this should not lead us in not programming null safe.

3. JEP 368: Text Blocks (Second Preview)

In many languages, like Kotlin or Swift, it is possible to define text blocks without concatenate strings together.

This results in much more readable code. Until now, Java hasn’t such a feature. Also in JDK14 it is still a preview. (to enable the preview --enable-preview) With JDK15 it is actually planned to leave the preview status. So it’s worth to take a look at this preview feature to be prepared.

In Java you define String-Blocks actually like this

String oldStyle = “<html>\n”
    + “    <head>\n”
    + “    </head>\n”
    + “    <body>\n”
	  + "    %s\n"
    + “    </body>\n”
    + “</html>”;
String.format(oldStyle, text);

With the new Java Text Blocks this will transform into code like this

var newStyle = """<html>
    <head>
    </head>
    <body>
        %s
    </body>
</html>
""".formatted(text);

formatted is a new method in String to simplify value substitution in text blocks.

As you can see, the code is much more readable.

More

There are a lot more interesting new features included in this JDK release. Have a look at the Records preview, which introduces a immutables concept to Java.

  • 305: Pattern Matching for instanceof (Preview)
  • 343: Packaging Tool (Incubator)
  • 345: NUMA-Aware Memory Allocation for G1
  • 349: JFR Event Streaming
  • 352: Non-Volatile Mapped Byte Buffers
  • 359: Records (Preview)
  • 362: Deprecate the Solaris and SPARC Ports
  • 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector
  • 364: ZGC on macOS
  • 365: ZGC on Windows
  • 366: Deprecate the ParallelScavenge + SerialOld GC Combination
  • 367: Remove the Pack200 Tools and API
  • 370: Foreign-Memory Access API (Incubator)

OpenJDK JDK14