Forget Java 11, Java 17 is for you

Tram Ho


Today I would like to introduce the outstanding new features from java 11 onwards, helping you to be more confident to upgrade to java 17.

Text Blocks (Java 15)

Text Blocks is a feature developed from java 13, but it was not until java 15 that it was officially put into use. Text Blocks make java string usage easier

Java old version

Java new version

To use variable values ​​in Text Blocks you can use the String.format function, for example:


record is a new type in java 15 that helps you to quickly initialize objects.
Let’s make an example to see the convenience compared to using Class

Using Class, initialization is relatively long and cumbersome =))

Using Records, the syntax is very compact and without the need for a 3rd party library like lombok, the name and age variables will be stored by the record.

Sealed Classes

Classes when declared in java will not control inheritance, any Class can inherit your Class, to overcome this problem Sealed Classes were introduced in java 15.

Sealed Classes control inheritance, classes can only inherit Sealed Classes when Sealed Classes grant inheritance

When using permits ClassA, ClassB you are granting permissions to ClassA, ClassB is allowed to inherit from classes Person any other class when inheriting will receive an error message.

Pattern Matching Type Checks

From java version 14 and above, checking Type will become syntactically simpler, this change usually doesn’t make a difference in performance but makes your code a lot more transparent.

Vector API

The Vector API was first developed in java 16. The idea of ​​this API is to provide a means of vector computation that will eventually be able to perform more optimally than traditional scalar computation methods.

This calculation example, for an array of length 4, will execute in 4 cycles. Now, let’s look at the Vector-based calculation:

Vector helps you to be more optimal when you work with arrays.

Share the news now

Source : Viblo