The Complete Guide to Java 12 New Features
6 months flew by so fast, and again, it’s time to take a closer look at the new JDK version that’s out. Let’s meet Java 12 and the features it’s introducing to developers.
It’s a been a while since Oracle introduced its accelerated 6-month release cadence, and it’s getting harder to keep up with every version and the features they add to the table.
On the bright side, this fast release rate also means that Java is introducing a lot of new features for us to take advantage of. This means that we need to pay attention all the time to see what exciting capabilities are being added. And now it’s time to see what Java 12 is all about.
New Java 12 Features
Shenandoah: A Low-Pause-Time Garbage Collector
This experimental feature wants to add a new garbage collection (GC) algorithm, named Shenandoah, made for applications who prefer responsiveness and predictable short pauses.
This feature introduces an algorithm which is efficient enough to allow programs to run in the available memory but also optimized to never interrupt the running program for more than a handful of milliseconds.
Shenandoah algorithm will help reduce GC pause time by doing evacuation work concurrently with the running Java threads. It trades concurrent CPU cycles and space for pause time improvements, and the pause times are independent of heap size, meaning you will have the same consistent pause times whether the heap is 200 MB or 200 GB.
Oracle does note that this is not about fixing all JVM pause issues, as these could be caused due to Time To Safe Point (TTSP) issues or even to monitor inflation of the application. And for these cases, the algorithm would not be relevant.
Switch Expressions
This JEP is meant to extend the switch statement; a multi-way branch statement that provides an easy way to dispatch execution to different parts inside the code, based on the value of the expression.
This expansion, currently in preview mode, enables us to use switch as either a statement or an expression, to simplify coding and prepare for another upcoming JEP, Pattern Matching for instanceof (305).
To understand what it means, Oracle shared the following examples. The unnecessarily verbose and visually noisy code often masks hard to debug errors, where missing break statements indicate that accidental fall-through occurs:
The new switch label, written “case L ->” will signify that the code to the right of the arrow is only to be executed if the label is matched. So the previous code can now be written:
G1 Improvements
Java 12 comes with 2 updates to the default garbage collector, G1. The first one, titled “Abortable Mixed Collections for G1” helps make G1 mixed collections abortable if they might exceed the pause target.
Currently, G1 uses an analysis engine to select the amount of work to be done during a collection, and once the collection set has been determined and the collection has been started then G1 must collect all live objects in all regions of the collection set without stopping. This leads to the garbage collector exceeding the pause time goal, the issue this JEP aims to solve.
The other G1 feature also focuses on improving the performance of the garbage collector, specifically returning unused committed memory.
The current version of G1 only returns memory from the Java heap at either a full GC or during a concurrent cycle. Since G1 tries hard to avoid full GCs completely, and only triggers a concurrent cycle based on Java heap occupancy and allocation activity, it will not return Java heap memory in many cases unless forced to do so externally.
That’s where “Promptly Return Unused Committed Memory from G1” comes in, focusing on enhancing the garbage collector by automatically returning Java heap memory to the operating system when G1 is idle.
What Else?
Other features that are introduced in Java 12 are:
- Microbenchmark Suite – Adding a basic suite of microbenchmarks to the JDK source code, making it easy for developers to run existing microbenchmarks and create new ones.
- JVM Constants API – Introducing an API to model nominal descriptions of key class-file and run-time artifacts, such as constants that are loadable from the constant pool. This API will be useful for tools manipulating Java classes and methods.
- Default CDS Archives – Enhance the JDK build process to generate a class data-sharing (CDS) archive and improve out-of-the-box startup time.
Is It Worth It?
With constant updates we see more and more features introduced into the language – but what are the developers feeling about this recent change? It’s not always easy to upgrade and implement new language features in large systems and enterprises, not to mention creating compatibility with legacy code and local tricks and hacks made by the team.
And indeed, when we take a cold, hard look at recent Java versions, we see that the frequent releases do not mean that the most recent version is the most popular one. And by looking at Google Trends, we can see that it isn’t so:
Although being the “oldest” member of the family, Java 8 popularity is undeniable. Java 11 is gaining some traction, probably due to it being the latest long-term supported version, while Java 12 is at the bottom of the list. It is a new version, so we’re willing to give it some time, but judging from the current state, it looks like it’s not about to change much.
Does that mean that Oracle should stop their six months cadence? Well, no. This decision was made to allow Oracle release “smaller” features faster than before, helping Java align with other languages on the market.
If you ask us, this change only helps Java maintain its power and popularity, both for young and upcoming developers and veterans in the programming world. Now the only question we have is… what will Java 13 introduce, and how will those features change the way we code?
Published on Java Code Geeks with permission by Henn Idan, partner at our JCG program. See the original article here: The Complete Guide to Java 12 New Features Opinions expressed by Java Code Geeks contributors are their own. |
Looks like all these versions are big mess now. I can’t understand what is stable version, what is not. Should I update to latest or skip it…
Can anybody explain in two words, what all thes numbers mean? How to choose proper version?
Thanks