Ukraine needs your help!

Support
Series: What is new in Java?

What is new in Java 12?

Java

As usual, in this post, you can see the list of new features provided by Java 12 and a short demonstration part of them.

This post was published later, because of building the series “What is new in Java?” of releases of Java starting from Java 11.

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

Implemented and supported by RedHat for aarch64 and amd64, the Shenandoah Garbage Collector provides predictable and short garbage collection pauses regardless of heap size. The idea is to run the garbage collection at the same time as running Java streams.

This is an experimental function, to be able to use that you have to enable -XX:+UnlockExperimentalVMOptions and -XX:+UseShenandoahGC.

JEP 230: Microbenchmark Suite

Added Microbenchmark Suite, it extends the base pack of microbenchmarks to the source code of SDK. For developers, it simplifies running existing microbenchmarks and creating new ones.

JEP 325: Switch Expressions (Preview)

Provides an experimental feature for switch construction which improves using switch.

Have been added two major changes:

  1. New syntax construction case [..] -> allows do not provide break keyword to interrupt execution of switch.

    switch (number) {
      case 2, 4, 6 -> System.out.println("Pair numbers");
      case 1, 3, 5 -> System.out.println("Not pair numbers");
      default -> System.out.println("Other numbers");
    }
  • Also, you can use a switch as an expression now:
final String resultNumber = switch (number) {
  case 2, 4, 6 -> System.out.println("Pair numbers");
  case 1, 3, 5 -> System.out.println("Not pair numbers");
  default -> System.out.println("Other numbers");
}

JEP 334: JVM Constants API

Provided a new package java.lang.constant.

Classes and interfaces to represent nominal descriptors for run-time entities such as classes or method handles, and classfile entities such as constant pool entries or invokedynamic call sites.

API provides key model of class-file and run-time artifacts, such as constant poll. To get more detail info follow this link.

JEP 340: One AArch64 Port, Not Two

Provided two different packs of sources, where both ported for arm64. First, supported by Oracle uses arm64 (hotspot/cpu/arm), and second one, uses aarch64 (hotspot/cpu/aarch64).

JEP 340 removes the support arm64, and the whole source code used by #ifdefs in hotspot/cpu/arm will be removed too. 64-bit ARM build by default will be using aarch64.

JEP 341: Default CDS Archives

Enhance the JDK build process to generate a class data-sharing (CDS) archive, using the default class list, on 64-bit platforms.

The goal is to reduce startup time. Starting from Java 12, CDS is enabled by default.

Execute the java file using CDS, use this flag:

java -Xshare:off HelloWorld.java

JEP 344: Abortable Mixed Collections for G1

Make G1 mixed collections abortable if they might exceed the pause target. Once the collection set has been determined and the collection has been started then G1 must collect all live objects in all regions of the collection set without stopping.

JEP 346: Promptly Return Unused Committed Memory from G1

Enhance the G1 garbage collector to automatically return Java heap memory to the operating system when idle.

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022