Changes in Java12

Switch expression

It has been revamped to act as an expression. It removes the usage of break.

switch(day) {
  case SATURDAY, SUNDAY -> System.out.println(1);
  case TUESDAY, FRIDAY -> System.out.println(2);
  case THURSDAY, MONDAY -> System.out.println(3);
  case WEDNESDAY -> System.out.println(4);
}

Teeing collectors

Allows to collect two pieces of information from a stream pipeline.
For example, I want to obtain min and max from an Stream<Integer> to create a range.

final List<Integer> list = Lists.of(1, 2, 5, 8);
final Range<Integer> range = list.stream()
      .collect(Collectors.teeing(
           Collectors.minBy(Integer::compareTo),
           Collectors.maxBy(Integer::compareTo),
           // custom created Range factory.
           Range::ofOptional));

Misc.

API Changes

  • new String.format() method to indent and format Strings
  • better error recovery in CompletableFuture
  • new CompactNumberFormat to format numbers in a fashionable way
  • new method Files.mismatch(file1, file2), compares two files and returns the index of the first byte where they differ or -1

Shenandoah

New garbage collection (GC) algorithm made for applications who prefer responsiveness and predictable short pauses.

Reference(s)

https://blog.overops.com/the-complete-guide-to-java-12-new-features/
https://blog.codefx.org/java/java-12-guide/#Teeing-Collectors