What did Java 17 bring over Java 11 for a working Java developer?
answer
- a language built for data
- five features that previewed first
- a closed set of subtypes
- reflection into the JDK stops working
- what was still preview at 17
basics
~20 sOver Java 11, Java 17 has final switch expressions, text blocks, records, instanceof patterns and sealed classes, plus Stream.toList and helpful NullPointerException messages. It removed CMS and Nashorn and strongly encapsulated JDK internals, which is the common upgrade break.
solid answer
~40 sThe headline is data modelling. Against Java 11, Java 17 has final switch expressions, text blocks, records, pattern matching for `instanceof` and sealed classes, and each of them previewed first. In libraries I use `Stream.toList()` and `String.formatted`. Helpful `NullPointerException` messages name what was null, and they are on by default since Java 15. ZGC and Shenandoah became production features in Java 15, and CMS was removed in Java 14. The upgrade risk is strong encapsulation: `--illegal-access` is obsolete at 17, so deep reflection into JDK internals fails unless the package is opened, for example with `--add-opens`. Pattern matching for `switch` is still a preview at 17, so sealed types only pay off fully at Java 21.
code
java · 33 linespublic class Java17Idioms {
sealed interface Payment permits Card, Transfer {}
record Card(String last4) implements Payment {}
record Transfer(String account) implements Payment {}
enum Size { S, M, L }
static String describe(Payment p) {
if (p instanceof Card c) {
return "card " + c.last4();
}
return "transfer " + ((Transfer) p).account();
}
static int price(Size size) {
return switch (size) {
case S -> 5;
case M -> 8;
case L -> 11;
};
}
public static void main(String[] args) {
String json = """
{"size": "M"}
""";
System.out.print(json);
System.out.println(describe(new Card("4242")) + " costs " + price(Size.M));
System.out.println(new Transfer("ACC-1"));
}
}go deeper
Recall the five language features and what each is for: switch expressions, text blocks, records, instanceof patterns and sealed classes. Be able to write a small record and a text block.
Explain each feature's mechanics and when it became final, and explain why strong encapsulation breaks reflective code at 17 and what --add-opens does about it.
Show what the upgrade costs in production: libraries that reflect into the JDK, CMS flags that must go, the effect of disabled biased locking, and how you would test before moving a fleet.
Weigh --add-opens as a debt rather than a fix: every flag is a dependency on an internal API. Decide which libraries must move before the platform does, and which features the team should standardize on.
## The language is the headline Java 17, released in September 2021, is the point where the data-modelling features that previewed across Java 12 to 16 are all final. Each one went through preview rounds first, so it matters which release you name. | Feature | Previewed in | Final in | JEP | What it gives you | |---|---|---|---|---| | Switch expressions | 12, 13 | 14 | 361 | `switch` yields a value, and `case X ->` has no fall-through | | Text blocks | 13, 14 | 15 | 378 | Multi-line string literals, such as a JSON payload in a test | | Records | 14, 15 | 16 | 395 | Transparent carriers for immutable data, with generated members | | Pattern matching for `instanceof` | 14, 15 | 16 | 394 | Test and bind in one step, with no cast | | Sealed classes | 15, 16 | 17 | 409 | The class author controls which types may extend it | These combine. A sealed interface with record implementations is a closed data model. At Java 17 you still cannot switch over it by type without enabling preview features, because pattern matching for `switch` is only a preview there (JEP 406). The compile-time check that every permitted type is handled arrives in final form at Java 21. A record's fields are final, but final is not deep. A record holding a `List` still holds a list someone else may change. That is a common follow-up. ## The libraries and tools - **`Stream.toList()`** (Java 16) returns an unmodifiable list. - **`String.formatted`** (Java 15) and **`Collectors.teeing`** (Java 12) tidy up common code. - **`Stream.mapMulti`** (Java 16) replaces one element with any number of elements, without creating a stream per element. - **`RandomGenerator`** (Java 17, JEP 356) gives a uniform interface over random number generators, and **`HexFormat`** (Java 17) converts bytes to and from hex. - **`jpackage`** (Java 16, JEP 392) packages an application into a platform-specific package with its dependencies. ## The runtime and diagnostics - **Helpful `NullPointerException` messages** (JEP 358) arrived in Java 14 behind a flag and are on by default since Java 15. They have a question of their own. - **ZGC and Shenandoah** became production features in Java 15 (JEPs 377 and 379). Both were experimental before that. - **CMS is gone** (JEP 363, Java 14). A service still passing `-XX:+UseConcMarkSweepGC` needs a new collector choice. - **Biased locking** was disabled by default at Java 15 by JEP 374, which warns that some applications may see a performance regression. Java 18 then made its options obsolete, and JDK 25 and 27 refuse to start with `-XX:+UseBiasedLocking`. - **Elastic Metaspace** (JEP 387, Java 16) returns unused class-metadata memory to the operating system more promptly. - **JFR event streaming** (JEP 349, Java 14) lets a program consume Flight Recorder data continuously. ## What can break on the way up Strong encapsulation is the change to check first: - At Java 11, `--illegal-access=permit` was still the default, so class-path code could reach JDK internals in packages that existed in JDK 8, with one warning on the first reflective access. - At Java 16, the default became `deny` (JEP 396). - At Java 17, the option is obsolete: using it has no effect beyond a warning (JEP 403). Deep reflection into JDK internals now throws `InaccessibleObjectException` unless a specific package is opened with `--add-opens`, or with the `Add-Opens` manifest attribute of a JAR started by `java -jar`. One exception matters: `sun.misc` and `sun.reflect` stay accessible through the `jdk.unsupported` module, so `sun.misc.Unsafe` keeps working at 17. Other removals to check for are the Nashorn engine (JEP 372, Java 15), the Pack200 tools (JEP 367, Java 14), RMI Activation (JEP 407, Java 17) and the experimental ahead-of-time and JIT compiler (JEP 410, Java 17). The Security Manager and the Applet API were deprecated for removal at 17, with JEPs 411 and 398. ## What was not final at 17 - Pattern matching for `switch`: preview (JEP 406). - Foreign Function & Memory API: incubator (JEP 412). - Vector API: second incubator (JEP 414). Calling any of these a Java 17 feature without the qualifier is the mistake an interviewer listens for. ## How to answer Lead with the data-modelling features and say which you use. Then name what you had to fix on the way up, usually a library reflecting into the JDK or a CMS flag. Finish with what 17 still lacked, because that shows you know why teams moved on to 21.
- How does Stream.toList() differ from collect(Collectors.toList())?`Stream.toList()` is documented to return an unmodifiable list, and mutators throw `UnsupportedOperationException`. `Collectors.toList()` gives no guarantee about the type or mutability of its list. So code that adds to the result may need `Collectors.toCollection(ArrayList::new)`. Note that `Stream.toList()` accepts null elements, unlike `List.of`.
- A library fails on Java 17 with InaccessibleObjectException. What are your options?First look for a library version that no longer reflects into JDK internals, since that is the lasting fix. As a stopgap, open the one package it needs, such as `--add-opens java.base/java.lang=ALL-UNNAMED`, or, if it starts with `java -jar`, set `Add-Opens` in that JAR's manifest, which other JARs ignore. `--illegal-access` no longer helps at 17.
- What is a sealed class good for at Java 17, if pattern matching for switch is still a preview there?It already lets the author control who may extend a type, so a hierarchy stays closed without making the classes final or package-private. Code can rely on that closed set. The compiler's coverage check over sealed types and patterns is what arrives in final form at Java 21.
- Records became final in Java 14.
- Pattern matching for switch is a final feature at Java 17.
- --illegal-access=permit still opens JDK internals on Java 17.
- A record's final fields make everything it refers to immutable.
- CMS is still selectable at Java 17, only deprecated.
- sun.misc.Unsafe became inaccessible at Java 17.
- Records
- Java 16pre-release 14
- Sealed classes
- Java 17pre-release 15
- Switch expressions
- Java 14pre-release 12
- Text blocks
- Java 15pre-release 13
- Pattern matching for instanceof
- Java 16pre-release 14
Assumes JDK 27 as the baseline and compares Java 17 with Java 11; each release number says when the feature became final, and all are still final at 27.
context
- JEP 395: Recordsopenjdk.org
- JEPs in JDK 17 integrated since JDK 11openjdk.org
- JEP 403: Strongly Encapsulate JDK Internalsopenjdk.org