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