What Is New in Java 12
| Category | Feature/Change |
|---|---|
| New Features | Switch Expressions (Preview), Shenandoah GC (Experimental) |
| Improvements | Microbenchmark Suite, Default CDS Archives |
| APIs | New Methods in String, Files, Collectors, and NumberFormat |
| JVM | Abortable Mixed Collections for G1, Promptly Return Unused Committed Memory |
What are the new language features in Java 12?
Java 12 introduced Switch Expressions as a preview feature. This allows the entire switch block to be used as an expression, making code more concise and less error-prone. You can now use multiple case labels and return a value directly from a switch case.
In practice, this reduces boilerplate code significantly. The traditional colon syntax with break statements can be replaced with a cleaner arrow syntax that doesn't cause fall-through.
// Traditional way
int numLetters = 0;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
// ...
}
// New switch expression (preview)
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
// ...
};
What garbage collection improvements were added?
Java 12 shipped two major GC enhancements. The Shenandoah garbage collector was added as an experimental feature, designed to reduce pause times by doing evacuation work concurrently with running Java threads. This is a low-pause-time collector that works well for large heaps.
For the G1 garbage collector, abortable mixed collections were implemented. If G1 discovers that a mixed collection will exceed the pause time goal, it can abort the collection, making the collector more responsive to configured pause time targets.
Another G1 improvement automatically returns Java heap memory to the operating system when idle. During periodic garbage collections, G1 checks for unused memory regions and returns them to the OS, improving memory utilization in containerized environments.
What new API methods should developers know about?
The String class gained four new methods for manipulating text. indent() adjusts the indentation of each line, while transform() applies a function to the string. The describeConstable() and resolveConstantDesc() methods support the Constants API.
Files.mismatch() was added to find the first differing position between two files. This is more efficient than manually comparing files byte-by-byte and handles many edge cases automatically.
Collectors.teeing() is a new collector that merges results from two downstream collectors. This enables collecting two different summaries of the data in a single pass through the stream.
// Finding the first difference between two files
long mismatch = Files.mismatch(path1, path2);
// Returns -1L if no mismatch, otherwise position of first difference
// Using teeing collector to get both count and sum
double average = stream.collect(Collectors.teeing(
Collectors.counting(),
Collectors.summingDouble(e -> e),
(count, sum) -> sum / count));
How does Java 12 improve startup performance?
The Class Data Sharing (CDS) feature was enhanced to generate the default archive file during application installation. Previously available only for the JDK, CDS archives now work with application classes, significantly reducing startup time for smaller applications.
This change means the JVM can memory-map the archive instead of loading classes individually. In practice, this can cut startup time by tens of milliseconds, which matters for command-line tools and short-lived microservices.
What about the new microbenchmark suite?
Java 12 added a suite of microbenchmarks to the JDK source code. These tests, based on the JMH framework, help ensure performance regressions are caught early during development.
While primarily for JDK developers, this suite demonstrates proper benchmarking techniques. Developers can study these tests to learn how to write effective microbenchmarks for their own code.
FAQ
Is the new switch expression syntax backward compatible?
Yes, the traditional switch statement syntax remains fully supported. The new expression syntax is an addition that doesn't break existing code.
Should I use Shenandoah GC in production?
Since Shenandoah is marked as experimental in Java 12, it's not recommended for production use. It's intended for testing and evaluation purposes in this release.
How do I enable the preview features like switch expressions?
You need to explicitly enable preview features using the --enable-preview flag both during compilation (javac --enable-preview) and runtime (java --enable-preview).
What happens if G1 aborts a mixed collection?
The aborted collection will be rescheduled for later. Meanwhile, G1 will continue to allocate memory normally, potentially causing the heap to grow slightly until the next collection.
Does Files.mismatch() work with large files efficiently?
Yes, the implementation is optimized to handle large files without loading them entirely into memory, using buffered comparison techniques.