Martin Ahrer

Thinking outside the box

Java 14 major language and API improvements

2024-08-22 2 min read Martin

Java ⑭ has been released in March 2020.

The following content is summarizing the most important changes to the Java API, the Java language, and the JVM introduced with Java ⑭.

For a more complete overview follow the links in the following sections referring to the official Oracle release documents.

Language improvements

The following describes selected improvements of the Java language. See Java Language Changes for Java SE 14

Java 14 is introducing the following language enhancements as developer preview.

  • JEP 305 is introducing Pattern Matching for instanceof and is eliminating boilerplate code.

  • JEP 359 introduces Record to reduce repetitive boilerplate code in data model POJOs.

We are not diving into the details of these preview features and defer that until they are moving from preview for general availability.

Switch Expressions

Switch Expression
DocumentState current = INITIALIZED;

var nextStates = switch (current) {
  case INITIALIZED, PROCESSED -> Set.of(REVIEWED, APPROVED);
  case REVIEWED -> Set.of(APPROVED);
  default -> throw new IllegalStateException("Arrived in final state"); (1)
};
1The compiler warns when cases are not exhausted. A default clause is required. However, for enum switch expressions that cover all known constants, the compiler inserts an implicit default clause.
Switch Expression Yield
DocumentState current = INITIALIZED;
var nextStates = switch (current) {
  case INITIALIZED, PROCESSED -> {
    System.out.println(current);
    yield Set.of(REVIEWED, APPROVED);(1)
  }
  case REVIEWED -> {
    System.out.println(current);
    yield Set.of(APPROVED);(1)
  }
  default -> throw new IllegalStateException("Arrived in final state"); (1)
};
1The yield statement can be used to produce the case value.
Labeled Switch Expression Yield
DocumentState current = INITIALIZED;
var nextStates = switch (current) {
  case INITIALIZED, PROCESSED: {
    System.out.println(current);
    yield Set.of(REVIEWED, APPROVED);(1)
  }
  case REVIEWED: {
    System.out.println(current);
    yield Set.of(APPROVED);(1)
  }
  default:
    throw new IllegalStateException("Arrived in final state"); (1)
};
1The yield statement can be used to produce the case value.

API improvements

The following describes selected improvements of the Java API. See the Java 14 release notes.

Null Pointer Exceptions

Readable Null Pointer Exception Message
String text = null;
try {
  text.indent(1);
} catch (NullPointerException e) {
  Assertions.assertEquals("Cannot invoke \"String.indent(int)\" because \"text\" is null", e.getMessage());
}

Accounting Currency Format Support

Currency Instance
var locale = new Locale.Builder()
    .setLocale(Locale.US)
    .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "cf-account")
    .build();

NumberFormat format = NumberFormat
    .getCurrencyInstance(locale);
String result = format.format(-3.27); (1)

API improvements

The following describes selected improvements of the Java JVM. See the Java 14 release notes.