Sitemap

JAVA-RELEASE/JAVA-12

Java 12: Features and Highlights

4 min readJun 16, 2023

--

Press enter or click to view image in full size

Java 12, released in March 2019, brought several new features and enhancements to the Java programming language. This article aims to provide a comprehensive guide to these features, with simple explanations, and code examples.

Features :

  1. JEP 325 : Switch Expressions
  2. JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
  3. JEP 230: Microbenchmark Suite
  4. JEP 334: JVM Constants API
  5. JEP 340: One AArch64 Port, Not Two
  6. JEP 341: Default CDS Archives
  7. JEP 344: Abortable Mixed Collections for G1
  8. JEP 346: Promptly Return Unused Committed Memory from G1

1. Switch Expressions (JEP 325)

Java 12 introduced a preview feature called Switch Expressions, which aimed to simplify the traditional switch statement and make it more flexible. This feature was later finalized in Java 14.

Traditional Switch Statement

Before Java 12, switch statements looked like this:

String day = "MONDAY";
String dayType;

switch (day) {
case "MONDAY":
case "TUESDAY":
case "WEDNESDAY":
case "THURSDAY":
case "FRIDAY":
dayType = "Weekday";
break;
case "SATURDAY":
case "SUNDAY":
dayType = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day: " + day);
}

Switch Expressions in Java 12

With Java 12, switch expressions were introduced to simplify the syntax and reduce boilerplate code. The new switch expression looks like this:

String day = "MONDAY";
String dayType = switch (day) {
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> "Weekday";
case "SATURDAY", "SUNDAY" -> "Weekend";
default -> throw new IllegalArgumentException("Invalid day: " + day);
};

As you can see, the new switch expression is more concise and easier to read. It also eliminates the need for break statements.

2. Shenandoah: A Low-Pause-Time Garbage Collector (JEP 189)

Shenandoah is a new garbage collector introduced in Java 12 as an experimental feature. It aims to reduce garbage collection pause times by performing more work concurrently with the running Java threads. Shenandoah is particularly useful for applications that require low latency and predictable response times.

Get Pravinkumar Singh’s stories in your inbox

Join Medium for free to get updates from this writer.

To enable Shenandoah, use the following JVM options:

-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC

3. Microbenchmark Suite (JEP 230)

Java 12 introduced a new microbenchmark suite to help developers measure the performance of small code snippets. This suite is based on the Java Microbenchmark Harness (JMH) and provides a set of benchmarks for the JDK itself. The microbenchmark suite helps developers identify performance regressions and improvements in the JDK.

To run the microbenchmark suite, follow the instructions in the OpenJDK Wiki.

4. JVM Constants API (JEP 334)

Java 12 introduced a new API to model nominal descriptions of key class-file and run-time artifacts, particularly constants that are loadable from the constant pool. This API provides a more efficient way to access and manipulate constants in the JVM.

Here’s an example of using the JVM Constants API:

import java.lang.constant.*;

public class ConstantApiExample {
public static void main(String[] args) {
ClassDesc stringClassDesc = ClassDesc.of("java.lang", "String");
MethodTypeDesc concatMethodTypeDesc = MethodTypeDesc.of(stringClassDesc, stringClassDesc);
MethodHandleDesc concatMethodHandleDesc = MethodHandleDesc.of(MethodHandleDesc.Kind.INTERFACE_VIRTUAL, stringClassDesc, "concat", concatMethodTypeDesc);
System.out.println("Class descriptor: " + stringClassDesc);
System.out.println("Method type descriptor: " + concatMethodTypeDesc);
System.out.println("Method handle descriptor: " + concatMethodHandleDesc);
}
}


// Output:

Class descriptor: java.lang.String
Method type descriptor: (java.lang.String)java.lang.String
Method handle descriptor: invokevirtual java.lang.String.concat(java.lang.String)java.lang.String

In this example, we create a `ClassDesc` for the `java.lang.String` class, a `MethodTypeDesc` for the `concat` method, and a `MethodHandleDesc` for the `concat` method handle. The JVM Constants API allows us to work with these descriptors in a type-safe and efficient manner.

5. One AArch64 Port, Not Two (JEP 340)

Before Java 12, there were two ports for the AArch64 architecture: one for the ARM64 Linux platform and another for the ARM64 Windows platform. Java 12 merged these two ports into a single AArch64 port, simplifying the maintenance and development of the AArch64 platform.

6. Default CDS Archives (JEP 341)

Class Data Sharing (CDS) is a feature that improves the startup performance of Java applications by sharing common class metadata between different Java processes. Java 12 introduced default CDS archives, which are generated automatically during the JDK build process. This means that users no longer need to create CDS archives manually using the `java -Xshare:dump` command.

To use the default CDS archive, simply start your Java application with the `-Xshare:auto` option:

java -Xshare:auto -jar myApplication.jar

7. Abortable Mixed Collections for G1 (JEP 344)

The G1 garbage collector is designed to provide low pause times and high throughput. However, in some cases, the G1 collector’s mixed collections (which collect both young and old regions) can cause long pause times. Java 12 introduced abortable mixed collections, which allow the G1 collector to abort a mixed collection if it is taking too long, thus reducing the pause time.

This feature is enabled by default and can be controlled using the following JVM options:

- `-XX:G1AbortMixedCollectionOnTimeout` (default: true)
- `-XX:G1MixedCollectionOverheadPercent` (default: 200)

8. Promptly Return Unused Committed Memory from G1 ( JEP 346)

Java 12 introduced a new feature for the G1 garbage collector that allows it to return unused committed memory to the operating system more promptly. This feature is particularly useful for applications that have significant fluctuations in their memory usage, as it helps reduce the overall memory footprint.

To enable this feature, use the following JVM option:

-XX:G1PeriodicGCInterval=<interval_in_milliseconds>

For example, to trigger a periodic GC every 5 minutes, use:

-XX:G1PeriodicGCInterval=300000

Conclusion

Java 12 comes with these new features and enhancements that improve the performance, efficiency, and readability of Java applications. Hope you liked it.

Peace!

Related reads : java-release

--

--