News from the Java world, a new version of the Java Development Kit (JDK) is here, namely JDK 21!
In this TechUp, we want to address the following questions:
- What’s new with JDK 21? β
- Collections with order? π
- String Templating in a completely different way? π€
- static void final whatever main(Something in here) {}? π€―
- Virtual Threading with Java? π€
JDK 21
JDK 21 is the latest LTS (Long Term Support) version of Java. This version was released in September 2023. Let’s take a look at the most important features and improvements.
So let’s get started!
First, after what felt like an eternity until brew upgrade
finally finished, I installed the JDK 21 from Temurin for my MacBook.
By the way, you can find all code examples as complete projects here in the b-nova-techhub/java-21 repository.
A little tip on the side: For the complete nerd experience, check out https://javaalmanac.io/. π
String Templates (Preview)
JEP 430: String Templates (Preview)
Those who have followed the latest Java releases will surely immediately think “String Templates or Interpolations again?”. In the last versions, it was often about multi-line strings, now it’s specifically about being able to define dynamic areas in a string more easily and beautifully.
Let’s look at how it would have been done before:
|
|
We see three different possibilities, which were certainly used extensively in the past.
Now there are so-called StringTemplates
, which provide us with a placeholder logic using the \{}
syntax.
The new solution would look like this:
|
|
Let’s look at some more examples!
|
|
You can see how we can very easily generate strings dynamically using a placeholder or even templating syntax. Method calls and calculations can also be directly incorporated into the string.
Besides STR
, there is also FMT
for specific formatting and RAW
to define the actual StringTemplate once and then call it using a process
method.
The possibilities in the future are certainly gigantic here, as it is very easy to write your own processors:
|
|
|
|
Here it is nice to see that we can not only use variables in the StringTemplate, but also call methods or, for example, perform calculations.
Sequenced Collections
JEP 431: Sequenced Collections
Another new feature that came with JDK 21 as a stable feature is the so-called “Sequenced Collections”.
So it is now possible, for example, to get the first or last element of a collection. And not as in the past via get(0)
or get(size()-1)
, but via the new methods getFirst()
and getLast()
.
Of course, this only works if the implementation of the collection also supports this by implementing the interface SequencedCollection
.
In the following code snippet, you can see that we can both read with getFirst()
and getLast()
and write with addFirst()
and addLast()
.
|
|
In addition, the interface also offers the methods removeFirst()
and removeLast()
.
It should be noted that the writing methods such as add*
and remove*
only work if the collection also supports this.
If it is an ImmutableCollection, an UnsupportedOperationException
will be thrown.
Let’s take a quick look at the revised class diagram!
It is immediately noticeable that the probably most used interface List
now implements the interface SequencedCollection
.
This means that our code does not have to be adapted in most cases and we have direct access to the new methods.
Source: https://openjdk.org/jeps/431
You can also see that there is the analogous implementation as SequencedSet
and as SequencedMap
.
The new Main Method
JEP 445: Unnamed Classes and Instance Main Methods (Preview)
Hold on tight, buckle up, here we go! π¨π»βπ
With the preview feature JEP 445: Unnamed Classes and Instance Main Methods (Preview), THE! groundbreaking innovation comes in the now 65th release of Java.
There are now so-called “unnamed classes” and “instance main methods”. This means that we no longer have to define the main method in a class, but it can simply be written directly.
When I think back to my Java beginnings 12 years ago, always this memorization of public static void main(String[] args)
, that is now history!
|
|
This is a classic Java class with a main method, surely every Java newbie starts with such a class. But that is now a thing of the past.
|
|
And that’s all! No class, no package, no complicated parameters, just a main
method, as you know it from other languages.
The program is started as usual!
Hands-down, this is really a successful innovation for Java. This makes it easier to get started and makes the language more accessible, but it is also a welcome innovation for experienced developers if you want to quickly test a few lines of code.
The concept of the unnamed class is not entirely new, in past versions there were also unnamed modules and unnamed packages.
It is also exciting to mention here that you can define several main
methods with different parameters. This is another step towards flexibility and simplicity.
In this case, a corresponding launch protocol then decides which main
method is called.
Virtual Threading
Last but not least, certainly the most important feature of the JDK 21 release, the virtual threads!
Let’s assume we have an API that we need to query, each API call takes one second.
|
|
We have a long list of objects that we need to enrich with data from the API. The API has enough power, so we can work in parallel to get the data faster.
Until now, we have done this with Thread
or ExecutorService
, but that is not really efficient because the threads have too much overhead.
|
|
In the code snippet, we have 100 objects, for each object we start a “real” thread that queries the API. The execution of this program takes an average of 1100 milliseconds. (And we use String Templates, cool! π)
Now let’s look at the same example with virtual threads:
|
|
Not much has changed, the program also runs around 1100 milliseconds, but the difference is that we no longer use “real” threads, but “virtual” threads.
Using a diff, it is easy to see what the effective changes are:
So it is very easy to switch from a real to a virtual thread.
But now @Scale! What happens if we suddenly have 100,000 objects? So we adjust the number in the loop and restart the program.
The old solution runs: 19222 milliseconds, which corresponds to almost 20 seconds. Quite long! π΄
The new solution with virtual threads runs: 1722 milliseconds, which corresponds to almost 2 seconds. That is a huge difference!
Now let’s think this further, if we have, for example, a web server that has to process millions of requests per second, then that is a huge difference. Or is that only in theory?
Numerous benchmarks on the Internet show that virtual threads are significantly faster up to a certain point. From this point on, virtual threads perform equally well or even worse than “real” threads. You can find more information here.
Surely the question arises here how Java applications will be built in the future, classically sequentially or with real threads, according to the Reactive Programming principle or with virtual threads?
This question is difficult to answer, as it depends very much on the application and the requirements.
We will keep an eye on virtual threads in the future and will certainly experiment with RunOnVirtualThread
in one or the other Quarkus project.
If you want to learn more about virtual threads at this point, then I recommend this Quarkus article!
Varia
Briefly noted:
With Math.clamp() we can now check whether a value is within a certain range. If the value is outside, the corresponding min or max value is returned. That could be quite practical!
|
|
In addition, as in the last releases, something has changed in the Switch Statements, where you can now directly implement pattern matching without annoying instanceof checks. Additionally, there are now also further matching possibilities for Record Types.
LTS
It should be mentioned again here that this is an LTS version, which is supported for longer than the normal releases.
From my point of view, the change is worth it! You can find the complete list of all JEPs from the jump from the last LTS version Java 17 to JDK 21 here.
Conclusion
Cool and really practical features have been bestowed upon us with JDK 21! Virtual threads will certainly significantly improve the performance of web servers or even application servers in general! Sequenced Collections and String Templates are certainly features that we will use very often in the future.
Outlook
Java 22 is already in the starting blocks and is scheduled for release in March 2024. Currently, some features are already known, we will take a look at the most important and coolest features in the next TechUp! π JDK 23 is also already in the planning stage and there are already some exciting features awaiting us developers.
This techup has been translated automatically by Gemini