Javatpoint Logo
Javatpoint Logo

What is New in Java 12

The most recent long-term-support (LTS) release is Java 12, the first "interim" release since Java 11, which was published on March 19, 2019. After the LTS version 11 of Java, Java 12 was released. The 6-month release cycle applies to JDK 12. On March 19, 2019, Java 12-a Non-LTS version without long-term support-was made available. When compared to earlier versions, Java 12's modifications are rather minor. The language itself hasn't changed since Java 7 for the first time.

Java 12 is the name of an open-source reference implementation of the Java 12 SE Platform. Oracle offers GPL-compliant production-ready binaries for JDK 12, and other vendors' binaries will be made available soon.

There are not a lot of new API features in Java JDK 12. The primary advancements in JDK 12 pertain to internal Java/JVM. JEP, a new functionality, is included in JDK 12.

According to the significance of regular developer work, we have sorted the modifications. We will start by discussing improvements to the class library. Performance enhancements, experimental features, and preview features are included after that, and tiny changes that you probably won't see as a developer

Moreover, we'll talk about the new features in Java 12 and examine what it has in store for programmers.

Features in Java 12

Among the crucial Java 12 features are:

  • JEP 189, JEP 346, JEP 344, and JEP 230 concern changes to the JVM.
  • Change expressions
  • File mismatch() Technique
  • The Compact Number Format
  • Using the Stream API's Teeing Collectors
  • Indent(), transform(), describeConstable(), and resolveConstantDesc are new Java Strings methods ().
  • API for JVM Constants, JEP 334
  • JEP 305: Raw String Literals Pattern Matching is Removed from JDK 12

1) Changes in JVM

1.JEP 189 - A Low-Pause-Time Garbage Collector: Shenandoah (Experimental):

Shenandoah Garbage Collector was started by RedHat to shorten GC pause periods. The plan is to run the GC simultaneously with the active Java threads. It tries for predictable and consistent short pauses regardless of the heap size. Therefore, it is unimportant if the heap size is 15 MB or 15 GB. It is a beta function in Java 12.

An experimental garbage collection (GC) mechanism called Shenandoah is currently absent from the standard Java 12 releases.

By carrying out the evacuation job concurrently with the active Java threads, it decreases the GC stop times. This means that pause times with Shenandoah should be constant regardless of the size of the heap. A 200 GB heap of garbage should have a similar low pause behaviour as a 2 GB heap.

Since version 15, Shenandoah will be incorporated into the primary JDK builds.

2. JEP 346: Quickly Return Committed Unused Memory from G1:

When an application is not being used, G1 will now check the Java Heap RAM and return it to the operating system, according to Java 12. It is a preventative technique to preserve and utilize free memory.

3. Abortable Mixed Collections for G1 in JEP 344:

G1 mixed collections will now be abortable if they potentially go beyond the specified pause threshold, which will increase G1 efficiency. To do this, the mixed collection set is divided into mandatory and optional categories. In order to fulfil the target for the pause time, the G1 collector can prioritise collecting the required set first.

4. JEP 230 and 344:

The JDK source code now includes a minimal collection of microbenchmarks thanks to the Microbenchmark Suite, JEP 230 feature. Developers may easily construct new microbenchmarks and run the ones they already have because of this. JEP 344, One AArch64 Port, Not Two, keeps both the 32-bit ARM and 64-bit aarch64 ports while removing all sources relating to the arm64 port. As a result, developers can concentrate their efforts on creating a single 64-bit ARM implementation.

5. Default CDS Archives for JEP 341:

This improves the JDK build process so that it can create a class data-sharing (CDS) archive on 64-bit platforms using the default class list. The objective is to decrease starting time. Since Java 12, CDS is turned on by default. Do the following to execute your programme with CDS disabled:

2) New String and File Methods:

After the Java 11's Files and a few new String methods were introduced. For Java 12, the JDK developers enhanced both classes once again, adding readString() and writeString() capabilities.

1. indent():

We previously had to create a tiny helper method that would add the required number of spaces before the String in order to indent it. The procedure evolved into a more complicated one if it had to operate over numerous lines.

There is one method integrated into Java 12 called String.indent (). An illustration of how to indent a multiline string by four spaces is shown below:

2. transform():

The new String.transform() method transforms a String using any function and returns the result of the function. Here are a few illustrations:

There isn't any rocket science involved, as you can see from the source code of String.transform(). The String is supplied to the method's apply() method once the method reference has been translated into a function:

So why not just write the following instead of using transform()?

In contrast to the latter notation, which fixes the conversion at compile time, String.transform() allows the function to be applied to be dynamically determined at runtime.

3. mismatch():

To compare the contents of two files, use the Files.mismatch() method.

If both files match, the procedure returns -1. If not, it returns the first byte in which the two files diverge. The length of that file is returned if one of the files expires before a difference is found.

(A JDK enhancement proposal does not include definitions for the new string and files methods.)

  • The Teeing Collector

If your requirements call for it, you might want to combine the results of both collectors when terminating a stream with two rather than one collector.

To avoid being accused of having a "code smell," the following example source code aims to find the difference between the largest and the smallest number in a stream of random numbers:

With one exception, the programme builds but crashes during execution:

We learn from the exception text that we can only terminate a stream once.

Then, how can we accomplish this task?

A different approach would be to develop a unique collector that stores minimum and maximum values in a 2-element int array:

This strategy is not very readable and is pretty convoluted.

Using the "Teeing Collector," which Java 12 introduced, we can complete it more quickly. We can define two downstream collectors (collectors that combine the output of the two downstream collectors) and a merger function:

Much more readable and elegant, yes?

What does "Teeing Collector" mean in this context?

Since the collector's graphical depiction resembles a... "T," the name is derived from the English pronunciation of the letter "T":

What is New in Java 12

(There is also no proposal for the Teeing Collector JDK upgrade.)

  • Performance Improvements

The following improvements ensure that our Java applications start faster, have lower garbage collector latencies, and a better memory footprint.

Default CDS Archives:

In order to create the classes.jsa shared archive file, you previously had to run java -Xshare:dump once for each Java installation.

With the implementation of JDK Enhancement Proposal 341, all 64-bit JDK ports are now provided with this file, eliminating the requirement for Java applications to execute java -Xshare:dump and instead using the default CDS archive by default.

Abortable Mixed Collections for G1:

One of the objectives of the G1 Gargabe Collector is to stick to the maximum pause times that are set for cleanup activities that it cannot do concurrently with the program, i.e., to not stop the application for longer than the allowed time.

With the -XX:MaxGCPauseMillis argument, you can set this period of time for G1. If you remove the argument, the maximum allowed pause period is 200 ms.

To select a set of heap areas to clean up during this stop-the-world phase, G1 employs a heuristic (called the "collection set").

It is possible for the heuristic to determine a collection set that is too large, which causes the application to be interrupted for a longer period of time than planned, particularly in the situation of "mixed collections" (i.e., while cleaning up regions of young and elderly generations).

If the maximum pause duration is continuously exceeded, JDK Enhancement Proposal 344 optimizes the Mixed Collections to separate the collection set into a mandatory and an optional part. Up until the maximum pause duration is reached, the optional portion is done in small increments while the required portion is carried out without interruption.

The algorithm attempts to modify the heuristic in the interim in order to quickly select collection sets that it can process within the specified pause period.

Promptly Return Unused Committed Memory from G1:

The garbage collector should promptly return unneeded memory to the operating system in circumstances where you only pay for the memory you actually use.

Memory can be returned by the G1 garbage collector, but only when garbage is being collected. If the heap allocation or the current rate of object allocations does not start a garbage collection cycle, no memory is returned.

Let's say we have a program that, aside from running a memory-intensive batch procedure once per day, is otherwise largely inactive. As a result, after the batch operation is finished, there is no need for a trash collection cycle, and we pay for RAM that contains unused objects for the majority of the day (red highlighted area):

What is New in Java 12

JEP 346 offers an answer to this issue. A concurrent garbage collection cycle is regularly launched when the application is not in use, releasing any memory that may no longer be required.

By default, this function is deactivated. You can activate it by giving G1 a millisecond interval to check to see if such a cycle needs to start using the -XX:G1PeriodicGCInterval argument. It will swiftly retrieve the memory in this manner:

What is New in Java 12
  • Compact Number Formatting:

Introducing the CompactNumberFormat, a new number formatter in Java 12. Based on the patterns offered by a specific location, it's intended to express a number in a shorter format.

Through the getCompactNumberInstance method of the NumberFormat class, we may obtain its instance:

As previously mentioned, the locale parameter is in charge of supplying the appropriate format patterns. The format style has two options: SHORT and LONG. Let's use the example of the number 1000 in the US locale to better understand the format styles. The LONG format would be "10,000," while the SHORT format would be "10K."

Let's look at an illustration that uses two different styles to compact the number of likes for this article:

transform(Function f):

The specified function is applied to that string using the transform(Function f) method. Only one string value may be passed to the designated function, which will then output the object. To better comprehend this approach, let's look at an example.

  • Experimental and Preview Features:

Now here we are going to discuss the experimental and preview features i.e., functionality still in development that may be changed based on feedback from the Java community before the final release.

We will refer to the Java version where the corresponding features are released as "production-ready" rather than going into detail about these features.

Switch Expressions (Preview):

Let's contrast the previous and new switch statements as an example. Based on the DayOfWeek enum from the LocalDate instance, we'll utilise them to differentiate between working days and weekends.

JDK Enhancement Proposal 325 allows us to eliminate error-prone break statements from switch statements by utilising arrow notation and commas to separate numerous cases:

Let's examine the same logic using other expressions now:

Not only are new switch statements shorter and easier to read. The requirement for break statements is also eliminated by them. After the initial match, the code execution won't fail.

Another significant distinction is that we can directly apply a switch statement to the variable. Before, it was not feasible.

It's also feasible for switch expressions to run code without producing a result:

Notably, we have the option to use either the old or new syntax. Switch expressions in Java 12 are merely an addition; they do not take the place of anything.

Switch Expressions will be prepared for production in Java 14. The main article on switch expressions has all the information you need about them.

Switch Expressions must be enabled in Java 12 either in your IDE (IntelliJ allows you to accomplish this via FileProject StructureProject SettingsProjectProject language level) or by using the -enable-preview argument when executing the javac and java commands.

Unicode 11:

After Java 11 enabled support for Unicode 10, Java 12 upgraded that support to include Unicode 11. This means that the classes String and Character in particular must be able to handle the new characters, code blocks, and scripts that Unicode 11 adds.

See the part on Unicode 10 that was before referenced for an illustration.

(Unicode 11 compatibility is not included in any JDK enhancement proposals.)

Microbenchmark Suite:

Microbenchmarks for the JDK class library had been handled as a distinct project up until this point. These tests are used, for instance, to check that JDK methods haven't gotten slower with new Java releases. They periodically assess the JDK class library's performance.

To facilitate the execution and evolution of tests, JDK Enhancement Proposal 230 integrates the current set of microbenchmarks into the JDK source code.

  • JVM Constant API

Constants that emerge during the compilation of file are contained in the constant pool of a.class file. These include, among other things, the names of referenced classes and methods (such as "java/lang/System," "out," and "println") as well as constants that are defined in the Java code, such as the string "Hello world!" Each constant is given a number that is referenced in the bytecode of the.class file.

It will be simpler to develop Java programmes that read or write JVM bytecode thanks to JDK Enhancement Proposal 334. It offers new classes and interfaces to represent the elements in the constant pool for this purpose.

Constants serving as references to classes and their methods add to the complexity. Since we only know the names, parameters, and return values of the classes and methods that are being referred, we are unable to use the reflection classes Class and MethodHandle.

The classes ClassDesc and MethodHandleDesc and MethodTypeDesc are now available for this purpose, among others, to identify a class and a method, respectively.


The modifications in Java 12 are rather easy to handle. The Teeing Collector, which enables us to terminate a Stream over two collectors and merge their findings, as well as a few additional String and Files methods, are now available.

Thanks to the classes.jsa shared archive file that is available on 64-bit systems, class data sharing is now turned on by default.

Mixed collections can be stopped by the G1 Garbage Collector if they take too long. Unused memory is swiftly returned to the operating system.

The Shenandoah Garbage Collector and Switch Expressions are two additional experimental or preview features that have made it into Java 12.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA