Java 12 features

Here’s a detailed yet simple list of the key features introduced in Java 12, along with explanations and code examples:


βœ… Top Features of Java 12

Released in March 2019, Java 12 was a short-term release with performance enhancements and some powerful new features for developers.


1. πŸ”Ή Switch Expressions (Preview Feature)

Introduced as a preview to simplify and extend the switch statement to return values.

Example:

String day = "MONDAY";
int num = switch (day) {
    case "MONDAY", "FRIDAY" -> 6;
    case "TUESDAY" -> 2;
    default -> 0;
};
System.out.println(num); // Output: 6

βœ… More concise and expressive than traditional switch.


2. πŸ”Ή Default CDS Archives

CDS = Class Data Sharing

Now automatically creates shared class metadata archives for faster JVM startup.

Benefits:

  • Faster startup time
  • Lower memory usage

No extra configuration needed β€” it works out of the box.


3. πŸ”Ή Shenandoah Garbage Collector (Experimental)

A low-pause-time GC developed by Red Hat, designed to reduce GC pause times regardless of heap size.

Enable using:

-XX:+UseShenandoahGC

4. πŸ”Ή JVM Constants API (for framework developers)

New APIs to model constant pool entries in class files β€” useful for tools, compilers, and frameworks.

Classes added:

  • Constable
  • ConstantDesc
  • DynamicConstantDesc

Not for general dev use, but important for tools like Javac, ASM, etc.


5. πŸ”Ή Microbenchmark Suite (JEP 230)

Java 12 added JMH-based microbenchmarking tools directly into the JDK source, to help JVM developers measure performance changes more accurately.

πŸ§ͺ Only relevant for OpenJDK contributors and performance engineers.


6. πŸ”Ή Abortable Mixed Collections for G1 GC

Improved pause-time predictability in G1 GC by allowing mixed collection sets to be aborted mid-way.

Enhances performance tuning and responsiveness of garbage collection.


7. πŸ”Ή Improved Prompt for jcmd VM.unlock_commercial_features

Better error message when using the old jcmd command that used to unlock commercial features (no longer required after Java 11).


πŸ”š Summary Table

Feature Description
πŸ” Switch Expressions (Preview) switch returns values; cleaner code
πŸ“¦ Default CDS Archives Improves startup time
🧹 Shenandoah GC (Experimental) Low-pause-time garbage collector
πŸ”£ JVM Constants API Modeling class-file constants
πŸ§ͺ Microbenchmark Suite Benchmarking JVM features
🚫 Abortable G1 GC Reduced GC pause spikes
πŸ“’ jcmd Prompt Improved Clearer messaging for deprecated commands

πŸ”§ Run Switch Expressions (Enable Preview)

If you want to use switch expressions in Java 12:

javac --enable-preview --release 12 Test.java
java --enable-preview Test


Back to blog

Leave a comment

Please note, comments need to be approved before they are published.