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.
String label = switch (day) { case SATURDAY, SUNDAY -> "weekend"; default -> "weekday"; };
Try it
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.
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.
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
yieldversusreturn. Inside a switch expression block useyield;return,breakandcontinuecannot 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
\nas the line terminator.\at the end of a line suppresses the newline;\skeeps a trailing space. - Records are shallowly immutable final classes with a canonical constructor, accessors (
name(), notgetName()),equals,hashCodeandtoString. 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 byreturn. - Sealed hierarchies list their permitted subclasses; each must be
final,sealedornon-sealed. Stream.toList()returns an unmodifiable list and allowsnullelements, unlikeCollectors.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.*andjdk.internal.*, and the removed Nashorn and RMI activation.