On 19th March 2019
Java12 was released. As we know Java12
was part of rapid release, it was released just in 6 months after Java11 release.
In this tutorial, we will go over all changes and new features about Java12.
Personally I switched to Java12 for all of my development but for production cycle it’s too early. Keep this tutorial bookmarked when you want to switch to Java12 for all of your production projects.
What’s new in Java12? New features in Java12:
There are quite a few internal and user workflow related features in Java12 which changed. Let’s take a look what is inside Java 12.
Change-1) Concurrent Class unloading
Normal Garbage Collector usually unloads unused variable during GC cycle and we usually notice some halt/pause in process, or CPU increase during that time. Usually we don’t even notice that.
With ZGC
(Z Garbage Collector) – Java12 supports concurrent Class unloading too. As this happens during normal GC cycle, there isn’t any pause and no more memory extra usage too.
By default ZGC is enabled in Java12. No more action required 🙂
How to disable ZGC?
- Just start your application with JVM command line argument
-XX:-ClassUnloading
Change-2) Get more details on JVM Crash
When there is a OOM (Out Of Memory) error or JVM crashes, usually Java creates dump files with all details.
-XX:HeapDumpPath=/tmp/crunchify/ -XX:+HeapDumpOnOutOfMemoryError
With this JVM parameters, Dump files will be created under /tmp/crunchify/
folder on OOM error.
There is one more option added in Java12:
-XX:+ExtensiveErrorReports
New log file will be created named hs_err<pid>.log
with all details about JVM crash. This is very helpful for your production environment if you are seeing frequent crash and want to debug more.
By default it’s disabled but you can enable extensive crash report by adding above JVM command line parameter.
Change-3) Compact Number Formatting
java.text adds support for compact Number format. 100o
can be mentioned as 1K
and 100000
can be mentioned as 100K
.
package crunchify.com.tutorials; import java.text.NumberFormat; import java.util.Locale; /** * @author Crunchify.com * Java12 Compact Number format example * */ public class CrunchifyJava12CompactNumber { public static void main(String args[]) { // NumberFormat is the abstract base class for all number formats. // This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are. NumberFormat crunchifyFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); // getCompactNumberInstance returns a compact number format for the specified locale and formatStyle. String crunchifyResult = crunchifyFormat.format(100000); System.out.println("NumberFormat.Style.SHORT Result: "+crunchifyResult); } }
Result:
NumberFormat.Style.SHORT Result: 100K
Change-4) Java Security Enhancements
security-libs/java.security
changes:
- disallow and allow Options for java.security.manager
- if
disallow
then System.setSecurityManager
can’t be used to set security manager.
- if
- -groupname Option Added to keytool Key Pair Generation
- a user can specify a named group when generating a key pair.
- Customizing
PKCS12 keystore
Generation- includes algorithms and parameters for
- key protection
- certificate protection
- MacData
- includes algorithms and parameters for
- New JFR Security Events
- What is JFR (Java Flight Recorder)
- 4 new JFR events added
- jdk.X509Certificate
- jdk.X509Validation
- jdk.TLSHandshake
- jdk.SecurityPropertyModification
Change-5) JEP 325: Switch Expressions
Enhanced Switch statement
is now supported in Java12.
- Java 12 based
case L -> syntax
operation. Here there isn’t any break necessary. - Use of Switch Expression
- this is simplified switch statement
- if a label is matched, then only the expression to the right of an arrow label is executed.
- No break statement needed.
CrunchifyJava12SwitchExample.java
package crunchify.com.tutorials; import java.util.Scanner; /** * @author Crunchify.com * What's new in Java12 Switch statement? * */ public class CrunchifyJava12SwitchExample { public static void main(String[] args) { Scanner crunchifyObj = new Scanner(System.in); log("Enter company name from: Google, Facebook, PayPal, eBay, Twitter, LinkedIn, Apple"); String company = crunchifyObj.nextLine(); log("Selected Company: " + company); // Pre-Java12 Switch statement switch (company) { case "Google": case "Facebook": case "PayPal": case "eBay": case "Twitter": log("Pre-Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter"); break; case "": case "Apple": case "LinkedIn": log("Pre-Java12: This switch is for companies Apple & LinkedIn"); break; default: log("Pre-Java12: Oops... Invalid company"); } /** * Java 12 based case L -> syntax operation. * Here there isn't any break necessary. */ switch (company) { case "Google", "Facebook", "PayPal", "eBay", "Twitter" -> log("Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter"); case "Apple", "LinkedIn" -> log("Java12: This switch is for companies Apple & LinkedIn"); default -> { log("Java12: Oops... Invalid company"); } } /** * This is switch expression */ final String companyName; companyName = switch (company) { case "Google", "Facebook", "PayPal", "eBay", "Twitter" -> ("Java12 Expression: This switch is for companies Google, Facebook, PayPal, eBay & Twitter"); case "Apple", "LinkedIn" -> ("Java12 Expression: This switch is for companies Apple & LinkedIn"); /** * it's also possible to do switch operation without a block and break */ default -> { break "Java12 Expression: Oops... Invalid company"; } }; log(companyName); } public static void log(String result) { System.out.println(result); } }
IntelliJ IDEA Result:
Enter company name from: Google, Facebook, PayPal, eBay, Twitter, LinkedIn, Apple Twitter Selected Company: Twitter Pre-Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter Java12: This switch is for companies Google, Facebook, PayPal, eBay & Twitter Java12 Expression: This switch is for companies Google, Facebook, PayPal, eBay & Twitter
Change-6) JVM Constants API
java.lang.invoke.constant
: As you may know, Java class has constant pool which stores all operands at runtime.
Java12 adds API for invoking constants at runtime.
Removed features from Java12:
Deprecated features from Java12:
Let me know if you have any handy tutorial on Java12 which you would like to include here.