Java 14 major language and API improvements
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.
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
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)
};
1 | The 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. |
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)
};
1 | The yield statement can be used to produce the case value. |
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)
};
1 | The 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
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
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)
1 | See the Unicode Currency Format Identifier. |
API improvements
The following describes selected improvements of the Java JVM. See the Java 14 release notes.