Java 12 was launched by Oracle on 19th, March 2019. It comes as a part of Java’s six monthly release cadence and it is a non-long term support (LTS) version contrary to Java 11 which was an LTS version. We will keep on getting the new releases every six months. This will help the developers in getting the access to the latest updates like new APIs and methods, JVM updates and so on.
Let’s have a look at the major features and enhancement introduced in JDK 12.
Read Also : Java 11 features and enhancements
1. String API Changes
1.1 String constantsIn JDK 12, two new interfaces, java.lang.constant.Constable and java.lang.constant.ConstantDesc have been implemented inside the String class. Each of these interfaces from Constants API has a method declared inside it with String class providing implementation for both of them. Let’s have a look at these two, newly introduced methods.
1.2 String.indent(int n) methodThis method adjusts the indentation of each line of the String based on the input value of n.
If the value of n is positive then it inserts n whitespaces at the beginning of each line. If the value of n is negative then it removes n whitespaces from the beginning of each line.
2. Switch Expressions (Preview) : (JEP 325)This is a preview language feature. It enables switch statement to be used as a statement as well as an expression. This feature paves the way for the usage of Pattern Matching in switch. Using switch expression will make the code more compact and increase it readability at the same time. The syntax is “case L ->”. Let’s check the switch expression example below.
The switch expressions do not have a fall through and there is no need of break statement in such cases. The code on the right side of the switch label can be a block, an expression or even a throw statement. It will only be executed if the label is matched. The “default” case has been made compulsory for switch expressions.
3. Shenandoah: A Low-Pause-Time Garbage Collector (Experimental) : (JEP 189)It is an experimental feature. Shenandoah garbage collector was initiated in 2014 by Red Hat and it aims at reducing the garbage collection pause times. It is designed to run concurrently with the other Java application threads. In Shenandoah GC, pause times do not depend on the heap size which means Shenandoah has short, consistent pause time and size of the heap does not matter.
4. Microbenchmark Suite : (JEP 230)A microbenchmark suite based on Java Microbenchmarking Harness (JMH) has been added to the Java source code. It will help the developers in creating new microbenchmarks as well as run the existing ones.
5. JVM Constants API : (JEP 334)A new java.lang.invoke.constant package has been introduced in Java 12 under JEP 334. As we know, there is a constant pool associated with every Java class file. It mainly stores the operands for bytecode instruction in the class. The entries in this pool are known as “Loadable constants” and they can describe simple values like strings or run time artifacts like methods and classes. A program that needs to manipulate the class files would need to model the loadable constants which not an easy task if the loadable constant describes a class. JVM Constants API would be of great importance in such cases as it has a pre-defined group of symbolic reference types which can define all types of loadable constants in a purely nominal form.
6. One AArch64 Port, Not Two : (JEP 340)Before Java 12, two ports existed for 64-bit ARM architecture in OpenJDK, one contributed by Oracle and another by Red Hat. As part of JEP 340, the port provided by Red Hat will be a default build as the build option for this port have been removed in Java 12. The 64 bit ARM implementation provided by Oracle will be removed. This has been done to remove the redundant effort required in maintaining two similar ports.
7. Default CDS Archives : (JEP 341)CDS stands for Class Data Sharing and it is one of the commercial features inside Oracle JDK. It has now been included in OpenJDK as well with a view to have both the binaries as same as possible.
For 64-bit version of JDK 12, there will be no need to run –Xshare:dump command explicitly to use the CDS feature. As part of this JEP, JDK build has been modified to run java –Xshare:dump command after the JDK image has been linked. This feature will also ensure a reduced start up time and memory footprint. Also, CDS will be ON by default from Java 12 onwards. The default shared archive file “classes.jsa” can be found inside the /bin/server folder. This file is the default CDS archive that has been generated using the default class list.
8. Abortable Mixed Collections for G1 : (JEP 344)This change has been introduced to make G1 garbage collector more efficient. G1 garbage collector divides the heap memory space into regions. Based on its pause prediction model and previous collection data, G1 predicts the number or set of regions that can be collected within the specified pause time target. These regions which are marked for collection are called “collection set”.
Before Java 12, if G1 starts the collection operation on a collection set, then it must finish the process without stopping. This could lead G1 into exceeding the pause time target set by the user if the collection set is large. It is here that the JEP 344 comes into picture. As per this feature, if the scenarios of selecting wrong number of regions are frequent, then G1 will do the collection work in steps, incrementally and it will abort the collection after every step if needed. This would ultimately result in G1 achieving a better pause time goal.
9. Promptly Return Unused Committed Memory from G1 : (JEP 346)This feature is again aimed at enhancing G1 garbage collector. In the current scenario, G1 returns Java heap memory back to operating system only in case of full garbage collection or during a concurrent cycle. G1 generally avoids full GC and the interval in which concurrent cycles are triggered is dependent on heap memory being currently used by the application. This may lead to instances where G1 does not release committed heap memory to operating system if the application is idle.
As part of JEP 346 in Java 12, G1 will now try to continue or start concurrent cycle periodically, if it finds the application to be inactive or idle. This will enable G1 to return the unused heap memory to the operating system in a more efficient way.
That's all for the java 12 features and new enhancements. Please mention in the comments if you have any questions related to the java 12.
String Java doc