Featured image of post New features when upgrading from Java 11 to Java 17

New features when upgrading from Java 11 to Java 17

In september 2021 Java 17 was released. Now, almost one year later, it is about time to upgrade. Java 17 is an LTS release with support until September 2023 and extended support until September 2026, which makes this version ideal for running in a production environment. We will walk you through the most important new features when upgrading from Java 11 to Java 17.

Before we begin, first some information about the JDK. An important change in Java 17 is that you are now allowed to use the Oracle JDK in production, for both commercial as well as personal use. Support for this JDK will end one year after the next Java LTS version has been released, so that is something to take into account.

Multi-line strings

The first new feature that is noteworthy is multi-line strings. You can now use three quotes to create such a multi-line string. The result is of course just a string. Within a multi-line string, you can just use newlines and quotes without the need to escape them. This way you can easily put some HTML, JSON, or SQL in your code. Multi-line strings were added in Java 15.

Records

Lombok is a very popular library within the Java ecosystem. That makes sense of course since generating and refactoring getters, setters and, constructors the whole time can become cumbersome. Let alone implementing the toString, hashCode, and equals methods. An alternative to Lombok is to use Kotlin’s data classes.

Since Java 14 you won’t need either Lombok or Kotlin’s data classes in most cases thanks to the introduction of records. Records are immutable data classes. To create one, just give the name of the Record and provide the types and names of its fields. The Java compiler automatically creates equals, hashCode, and toString methods for you, as well as private final fields, a public constructor, and getters for the fields.

Switch expressions

Switch expressions have been updated in Java 14 and are now shorter. You can now use the arrow syntax to immediately return a value, without using the break keyword. You can also assign a switch expression to a variable, making it nice and short:

If you prefer to use the old syntax, you can use the yield keyword to directly yield a value. You then no longer have to break out of the switch to prevent fall-through.

Z Garbage Collector is now stable

It was already available in Java 11, the Z Garbage Collector, but since Java 15 it received the label ‘Ready for production’. This low-latency garbage collector is now available for Linux, macOS, and Windows and does all its work in parallel. Also, the Z Garbage Collector doesn’t interrupt threads for more than 10ms. This makes it much more suitable for low-latency applications than the default garbage collector.

When you enable the Z Garbage Collector, it is important to set a good heap size. You must leave enough heap space so that living variables have space, as well as so that new variables can still be allocated. To enable and use the Z Garbage Collector in your application, you can use the following command when running it: java -XX:+UserZGC YourApplication.java.

Other features

Other new features in the past six Java releases are that now instanceof supports pattern matching. This means that in an if-statement you can check whether an object is an instance of and then immediately assign it to a variable. Here’s a sample:

if (o instanceof Car car) {
   System.out.println("Car: " + car.toString());
}

Furthermore, Java 14 improves the readability of null-pointer exceptions when using multiple chained methods. Java will now print which method returned null.

If you want to convert a Stream to a List, you always need to use the method .collect(Collectors.toList()). Since Java 16 you can now call the simpler .toList() on a Stream directly.

New features in Java 17

The features we’ve discussed so far are almost all introduced in earlier Java versions. These are the most important changes you will notice when upgrading from Java 11 to Java 17. However, Java 17 itself also contains some new things. One such improvement has been made to the pseudo-random number generator in Java, which is now easier to use using the new RandomGeneratorFactory class. Furthermore, Java 17 supports the Apple Silicon architecture on macOS and has support for sealed classes, useful when you are creating libraries. A sealed class gives you control over who can extend that class.

Conclusion

That’s it: when you are upgrading from Java 11 to Java 17, there are a lot of new features introduced in the last four years that you can now use. We’ve put together the most important. These new features make Java more modern and less verbose.