Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

Java 12 to 17 features

What Java 12 to 17 added: switch expressions, text blocks, records, pattern matching for instanceof, sealed classes, helpful NullPointerExceptions and Stream.toList.

Java 17 (September 2021, LTS) collects six releases of work. Many of its headline features first appeared as preview features in 12 to 16, so you may see them described under several version numbers. A preview is a complete feature that is switched on explicitly and can still change; it becomes standard when it graduates.

The result is a much more compact Java: expressions for switch, multi-line strings, small immutable data classes, type tests that bind a variable, and closed class hierarchies the compiler can check.

Java
String label = switch (day) {
    case SATURDAY, SUNDAY -> "weekend";
    default -> "weekday";
};

Try it

Java Switch expressions, yield and text blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Records, pattern matching for instanceof, sealed types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Helpful NullPointerException messages, HexFormat and RandomGenerator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Feature timeline

Version Highlights
12 Switch expressions (preview), Collectors.teeing, String.indent/transform, CompactNumberFormat, Files.mismatch, Shenandoah GC (experimental).
13 Text blocks (preview), yield (preview), dynamic AppCDS archives.
14 Switch expressions final, records and pattern instanceof (preview), helpful NullPointerException messages (opt-in), jpackage (incubator).
15 Text blocks final, sealed classes (preview), hidden classes, ZGC and Shenandoah production-ready, Nashorn removed, helpful NPE messages on by default.
16 Records and pattern instanceof final, Stream.toList(), Unix domain sockets, jpackage final, Vector API (incubator), strong encapsulation by default.
17 (LTS) Sealed classes final, strongly encapsulated JDK internals (--illegal-access removed), HexFormat, new RandomGenerator interface family, Security Manager deprecated for removal, macOS/AArch64 port, strictly-IEEE floating point (strictfp redundant).

Usage notes

  • yield versus return. Inside a switch expression block use yield; return, break and continue cannot leave a switch expression.
  • Exhaustiveness. A switch expression must cover every possible value. For enums and sealed types the compiler checks it; otherwise add default.
  • Arrow versus colon. case X -> does not fall through. Do not mix both styles in one switch.
  • Text blocks strip common indentation and trailing spaces, and treat \n as the line terminator. \ at the end of a line suppresses the newline; \s keeps a trailing space.
  • Records are shallowly immutable final classes with a canonical constructor, accessors (name(), not getName()), equals, hashCode and toString. They cannot extend a class or declare instance fields beyond the components.
  • Pattern variables are definitely assigned only where the match must have succeeded, hence !(o instanceof String s) followed by return.
  • Sealed hierarchies list their permitted subclasses; each must be final, sealed or non-sealed.
  • Stream.toList() returns an unmodifiable list and allows null elements, unlike Collectors.toUnmodifiableList().
  • Preview features in these versions (switch patterns, record patterns) were finished later; see Java 18 to 21.
  • Migrating from 11 to 17: watch for the strong encapsulation of sun.* and jdk.internal.*, and the removed Nashorn and RMI activation.