Java 16-what's new?

Java 16-what's new?

A new version of Java has just appeared, that’s Java 16. Some time ago we wrote about what was included in JDK 16, that is, the implementation of changes that are introduced in this language before the official release of the new version, and there were quite a lot of them. Here we will expand the topic a little, focusing on all the news in more detail.

So in this article, we will cover things like language changes, support extensions, internal orders, and ports and tools.

Without further ado, let us see what is happening in Java today.


Records

With the new version, Java has been enriched with records that have already appeared in versions 14 and 15 as preview. Records are classes that function as transparent containers of non-mutable data. The motivation here is that in Java, creating a class that holds only data required much more attention than is necessary. Defining accessories or methods such as equals(), hashCode(), toString() in this case, it is a source of frustration and error.

A good example that illustrates the possibilities of records can be found in JEP 359:

record Range(int lo, int hi) {
  public Range {
    if (lo > hi)  /* referring here to the implicit constructor parameters */
      throw new IllegalArgumentException(String.format("(%d,%d)", lo, hi));
  }
}

A more detailed description of the records can be found here.


Pattern Matching from instanceof

This change was first proposed in JEP 394 – we wrote about it on the occasion of the release of Java 14, and now it has already appeared as a stable feature. Here, pattern is understood as a set of predicates to be executed on an object and associated variables that will be extracted from the object if the predicate is true. Here’s what it looks like:

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

The following things happen here-we check whether obj is an instance String – if so, it is created s and the result of the projection is assigned to it obj na String.

A more detailed description of pattern matching can be found here.


Sealed classes

Sealed classes appear in Java 16 as a second preview. They were proposed in JEP 397, and their purpose is to restrict which classes, or interfaces, will be able to extend or implement sealed classes. It is also intended to allow the author of a class or interface to control which code is responsible for its implementation.


Improved cooperation with native programs

The foreign Linker API offers statically typed and typically Java native code access. This API, together with the Foreign-Memory API, has significantly simplify the process of creating bindings for native libraries, which in turn is very error-prone.

The purpose of introducing this API into Java is m.in. replacing JNI with a better Java solution. Moreover, the developers want to ensure interoperability with C libraries here.


Flexible metaspace

Flexible metaspace returns unused VM class metadata memory to the operating system in a fast manner. This will make Metaspace less memory intensive, but will simplify the code for this part.

Metaspace has had some problems with high heap memory usage in the past. So the goal here is to replace the current memory allocator with one that uses a twinning algorithm that partitions memory to make it easier to manage.

This approach has already been used, for example, in the Linux kernel.


Ports for new platforms

Java 16 supports Alpine Linux and other distributions that use musl as its main C library in the x64 and AArch64 architectures.

musl it is a Linux implementation of the standard library functions described in ISO C and POSIX. Alpine Linux, on the other hand, is a distribution widely used for deployment in the cloud, microservices and container environments due to the small size of the images.

Windows / AArch64 has become quite an important platform-it is now in great demand. Moving the JDK there is almost complete, but the purpose of this proposal is rather to integrate porting with the main JDK repository.


Other changes

As for the other changes that will be in Java, we have here m.in. migrating OpenJDK to Gita and GitHub, or warning classes based on values.

Java also adds a self-contained Java application packaging tool. It’s called jpackage and it’s been in the incubator for the last two editions.

Vector API also goes to the incubator. As the name suggests, it will help to work with vectors. It will probably change before the final release, so we will not give more details at this stage.

As for other changes in Java, here we also have support for Unix-domain socket. This is to extend the inherited channel mechanisms to support Unix-domain socket channels and server socket channels in Java.NIO.channels.

The new version will also support C ++ 14 functions in the JDK source code. This change is also intended to provide guidance on which features of this language should be used in the VM hotspot. It is worth noting that until JDK 15, C ++ functions in the JDK were limited to the C ++ 98/03 language standards.


Summary

Java 16 brought a lot of really interesting changes. First of all, it’s about records, which will make the lambook less necessary in many projects. Additionally stable release pattern matching with the help of instanceof this is so good news that many more JEPs depend on this feature.

Features from the incubator indicate that Java will work better with native code, which can create interesting opportunities.

Go to our cases Get a free quote