JDK 16 brings many 'small but significant' changes to Java

The latest features are about tightening up the functionality and cutting verbosity, with easier integrations coming down the line

Today sees the release of JDK 16, the eighth release of Java in three-and-a-half years. The release a number of improvements and extensions, including two language features pattern matching for instanceof and Records as full features and Sealed Classes which is now in second preview.

In a press conference call last week, Simon Ritter, Azul deputy CTO and a Java expert, and author and creator of the Java Specialists newsletter Heinz Kabutz, explained the new features, the thinking behind them and what they represent for developers.

In addition, the default build environment is moving from Mercurial to GitHub.

Pattern matching for instanceof removes some of the boilerplate required when developers need to verify if an object is of a certain data type. This has always been a two-stage process taking about three lines of code. The new feature makes it possible to do the same thing in the majority of use cases in one line.

Another highlight, Records, is also designed to cut down on Java's verbosity, said Ritter.

"The reason it's so useful is it makes the idea of simple data classes so much easier. If you just want a tuple where you have two values you want to encapsulate in a class, you've now got one line of code, whereas if you look at a standard data class with two instance variables in it that's 14 lines of code."

Records can also be used as an inner class, he went on. "It tidies up the code really nicely, you get much more readable code."

Meanwhile, Sealed Classes, now in second preview, allow developers to place restrictions on classes and interfaces that prevent other classes from extending or implementing them. This gives classes and interfaces more control over permitted subtypes. It also provides benefits in terms of ease of use, said Kabutz.

"What you can now do is you can specify up front which are the subclasses of an interface, and you can very easily, using Reflection, see where the available subclasses sit, which you could otherwise never do."

For the last three years, JDK releases have followed a fixed six monthly cadence, meaning that while not all the changes are big, said, Ritter, many are significant in the impact they will have.

Six-monthly releases have been a real success, he said. "We're seeing is a lot more features being delivered to Java a lot more quickly than we've ever seen before.

"We're also seeing some of the longer term projects that the OpenJDK community have been working on starting to filter through."

Among these are Project Panama, which aims to improve the connections between the Java virtual machine (JVM) and non-Java APIs, including many interfaces commonly used by C and C++ programmers. The Panama Project aims to replace the Java Native Interface (JNI), which Ritter commented "has always been a difficult API to work with", in part because its original designers wanted to keep everything within the Java ecosystem.

Asked which of the new features he was finding most useful so far, Kabutz mentioned the decision to move to another platform.

"If I had to pick my number one thing I like about what's happened it's the move to GitHub," he said.

OpenJDK provides a reference implementation of Java from which companies like Azul and Red Hat build their own distributions. Oracle also uses the code from the OpenJDK to build the Oracle JDK from which it creates Java SE (Standard Edition).