Upgrading from Java 11 to Java 17 (LTS)
Java 17 is an LTS release that brings language features, API enhancements, and platform changes. This guide focuses on what teams migrating from Java 11 should know: compatibility risks, useful new features, and a practical checklist to make the upgrade smoother.
What’s New in Java 17 (high level)
- Language: sealed classes (final), pattern matching for
switch(preview) - Platform / APIs: enhanced pseudo-random number generators, context-specific deserialization filters
- Strong encapsulation: JDK internal APIs are strongly encapsulated by default
- Removals / deprecations: Security Manager deprecated for removal; some legacy components removed
Sealed Classes (JEP 409)
Sealed classes and interfaces let you restrict which types may extend/implement them. This is useful for closed hierarchies that pair well with exhaustive switch handling.
1 | public sealed interface Shape permits Circle, Rectangle {} |
Notes:
- Permitted subclasses must be in the same module (or package when not using modules), and must be declared as
final,sealed, ornon-sealed.
Records (JEP 395 — standardized in Java 16)
A Java record is a concise way to declare immutable data carriers. Records were standardized in Java 16 (they appeared earlier as previews), and Java 17 includes them as a stable language feature.
1 | public record Point(double x, double y) {} |
Notes:
- Records automatically generate
equals,hashCode,toString, and accessors for components. - They are useful for DTOs, small immutable value classes, and work well with pattern matching and sealed hierarchies.
Text Blocks (JEP 378 — standardized in Java 15)
Text blocks provide a concise syntax for multiline string literals using triple quotes. They were standardized in Java 15 (after preview in earlier releases), and Java 17 includes them as a stable language feature.
1 | String json = """ |
Notes:
- Text blocks preserve newlines and simplify embedding JSON, SQL, HTML, and other multiline text.
- They also reduce the need for awkward string concatenation and manual escaping.
Pattern Matching for switch (Preview) (JEP 406)
Java 17 continues to evolve pattern matching by introducing it for switch (as a preview feature in 17).
1 | static String describe(Object obj) { |
To compile/run with preview features:
javac --release 17 --enable-preview ...java --enable-preview ...
Strong Encapsulation of JDK Internals (JEP 403)
In Java 17, most JDK internal APIs are strongly encapsulated. If your app (or a dependency) relied on sun.* / com.sun.* internal classes, you may see IllegalAccessError or warnings become errors.
Upgrade tips:
- Prefer supported public APIs.
- If you must, use JVM arguments like
--add-opens/--add-exportsas a temporary mitigation while migrating.
Enhanced Pseudo-Random Number Generators (JEP 356)
Java 17 adds new RandomGenerator interfaces and implementations to make PRNG selection more explicit and flexible.
1 | import java.util.random.RandomGenerator; |