Switch Expressions comes with Java 12

Switch Expressions comes with Java 12

Java 12, the latest six-monthly release of Java, has been released with support for Switch Expressions and the inclusion of the Shenandoah low-pause garbage collector.ย 

There are eight main improvements to the new release, starting with switch expressions. This is still a preview feature, and it extends the switch statement so that it can be used as either a statement or an expression. Whichever form is used, you can use either a traditional or a simplified scoping and control flow behavior.

Oracle says that in addition to simplifying everyday coding, theaddition prepares the way for the use of pattern matching in the switchstatement. Pattern matching is available as another preview languagefeature. Currently, the default flow of control of switch blocks is fallthrough, so requiring a break statement to avoid the rest of the Casestatements in a block being tested and potentially executed. The newswitch expressions introduce a new form of switch label, written “case L ->” to signify that only the code to the right of the label is to be executed if the label is matched. For example:

switch (day) {    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);    case TUESDAY                -> System.out.println(7);    case THURSDAY, SATURDAY     -> System.out.println(8);    case WEDNESDAY              -> System.out.println(9);}

The next improvements to the new release are to the current garbagecollector. From now on, the garbage collector will  automatically return Java heap memory to the operating system when idle rather than waitinguntil a full garbage collection to return memory to the operatingsystem, and it will also respect pause times better.

Until now, the garbage collector sometimes started an amount of workthat would take longer than the pause target, meaning the task overran.Under the improved version, if this is happening, the garbage collectorlowers its targets. Still on garbage collection, Shenandoah has been added in anexperimental version. Shenandoah is Java’s new low-pause-time garbagecollector algorithm. It reduces GC pause times by doing evacuation workconcurrently with the running Java threads. Pause times with Shenandoahare independent of heap size, meaning you will have the same consistentpause times whether your heap is 200 MB or 200 GB.

Another improvement is the addition of a basic suite ofmicrobenchmarks to the JDK source code, as well as making it easy fordevelopers to run existing microbenchmarks and create new ones. There’salso a new JVM Constants API that can be used to model nominaldescriptions of key class-file and run-time artifacts.

The OpenJDK open source release is available now, along with the commercial license version. JDK 12 will receive a minimum of two updates before being followed by Oracle JDK 13, which is due out in September 2019.

More Information

0
0