What's new in Java 21?
An overview of language and libbrary changes

Java 21
This is a real thing! Java 21 feels almost as a different programming language! Changes planned to be released in Sptember 2023 (in just a month!) can be roghly grouped into two categories:
Language and library changes

- sequenced collectios
- record patterns
- patterns in
switch
- virtual threads
- text patterns - preview
- unnamed paterns and variables - i.e. “underscore” - preview
- unnamed classes - preview
- scoped values - variables in dynamic scope (preview)
- structured concurrency - preview
Other changes

- generational ZGC
- API for vectors - 6th incubator
- API for securing symmetric keys
- Prepare To Disallow the Dynamic Loading of Agents
- Deprecate the Windows 32-bit x86 Port for Removal
- API to access FFI
Piece by piece
Sequenced collections
We will have new interfaces which would be able to correctly model the encounter order. The API we have so far (let me say this: all Collection API) is a bit chaotic: taking first or last element from different collections has different API:
Data Structure | First element | Last element |
---|---|---|
List | list.get(0) |
list.get(list.size() - 1) |
Deque | deque.getFirst() |
deque.getLast() |
SortedSet | ss.first() |
ss.last() |
LinkedHashSet | lhs.iterator().next() |
// missing |
The two new interfaces are: SequencedCollection<E>
:
|
|
and SequencedMap<K,V>
:
|
|
Record patterns
Pattern matching for Java… You could finally write something like this:
|
|
Patterns in ‘switch’
Expression that chooses the branch does not have to be of Enum type, and in case branches one can use qualified enumeration values:
|
|
Virtual threads
How to create them?
- by submitting tasks to a new executor
Executors.newVirtualThreadPerTaskExecutor()
(non-pooling, running each task in virtual thread, uses special internal fork-join pool) - new
Thread.Builder
class (early access JavaDoc API) - can be created using
Thread.startVirtualThread(Runnable)
(javadoc) - … or by using: Thread.ofVirtual()
What features they have?
- are always daemon threads (Thread.setDaemon(false) is just noop)
- have unchangable priority
- canno be assigned to thred groups
- public constructor always creates platform threads
Additionally
- Thread.isVirtual() - you can check what thread are you on
- Thread.getAllStackTraces() returns a map of platform threads, not all threads
Text patterns
Here we’ll have much more than string interpolation in other languages. And that’s good becasue we’re so late to the party…
Nice table from JEP 430:
Language | Code |
---|---|
C# | $"{x} plus {y} equals {x + y}" |
Visual Basic | $"{x} plus {y} equals {x + y}" |
Python | f"{x} plus {y} equals {x + y}" |
Scala | s"$x plus $y equals ${x + y}" |
Groovy | "$x plus $y equals ${x + y}" |
Kotlin | "$x plus $y equals ${x + y}" |
JavaScript | `${x} plus ${y} equals ${x + y}` |
Ruby | "#{x} plus #{y} equals #{x + y}" |
Swift | "\(x) plus \(y) equals \(x + y)" |
In Java we’ll be able to write:
|
|
or (html):
|
|
or (json):
|
|
Structured concurrency
Main class of interest is StructuredTaskScope StructuredTaskScope.
It allows to give a structure to the complex task which consists of many dependent, concurrent subtasks (function fork()
) and coordinate them: join()
is for whole group of subtasks forked from main task; we can use new APIjoinUttil(java.time.Instant)
and we can cancel (function shutdown()
) all tasks at once.
Pros: better error handling, thread cancellation propagation (but still no explicit context), readable code structure, readable thread structure in thread dumps:
|
|
Patterns and unnamed values
Where we don’t need a value, we don’t use it:
In switch
:
|
|
In for
:
|
|
…or in catch
blocks:
|
|
Unnamed classess
… are a simplification in writing ‘hello, world’. It would be sufficient for the compiler to compile “an unnamed class” when it encounters this code: taki kod:
|
|
A plan
In the next entries, I will check in more detail what these changes concern, describe them and I will use them in simple programs. Maybe I can do it before the September release?
Happy coding!
Pictures
Pictures genenrated w https://creator.nightcafe.studio/