Unlock Java 17: Master New JVM and Language Features with Real Code Examples
This article introduces the key enhancements in JDK 17—including improved NullPointerException messages, Metaspace reclamation, new garbage collectors, and language upgrades such as pattern‑matching instanceof, records, sealed classes, switch expressions, and text blocks—accompanied by practical code demonstrations.
1. Introduction
JDK 17, released on September 14 2021, is a long‑term support (LTS) version that builds on the previous LTS release JDK 11. The article lists the major additions from JDK 11 to JDK 17.
1.1 HotSpot JVM Improvements
Enhanced NullPointerException JVM now includes precise information about which variable is null, making NPE messages more useful.
Elastic Metaspace Unused Metaspace memory is returned to the operating system more promptly, reducing footprint and maintenance cost.
Terminating G1 Mixed Collections If a G1 mixed collection exceeds the pause target, it can be terminated early.
Returning Unused G1 Committed Memory G1 can now automatically return unused heap memory to the OS when idle.
Shenandoah GC Shenandoah moves from experimental to production status, providing low‑pause garbage collection.
ZGC ZGC, introduced via JEP 333 in JDK 11, is now a fully supported low‑latency collector.
1.2 Language Enhancements
Pattern‑matching instanceof Allows conditional extraction of components from an object directly in the instanceof test, simplifying code.
Records Introduces compact, immutable data carriers that automatically generate fields, constructors, equals, hashCode, and toString.
Sealed Classes and Interfaces Restricts which classes or interfaces may extend or implement a given type using the sealed and permits keywords.
Switch Expressions Switch can now be used as an expression, supports multiple case labels, and can yield a value directly.
Text Blocks Multi‑line string literals using triple quotes simplify embedding large text fragments without excessive escaping.
2. Practical Examples
2.1 Pattern‑matching instanceof
<code>if (obj instanceof Person person) {
// use person directly
}
if (obj instanceof Person person && person.getAge() > 10) {
System.out.printf("Name: %s%n", person.getName());
}
private int x;
private int y;
public final boolean equals(Object o) {
return (o instanceof Point p) && x == p.x && y == p.y;
}</code>2.2 Records
Traditional class definition:
<code>public class Point {
private final int x;
private final int y;
public Point(int x, int y) { this.x = x; this.y = y; }
// equals, hashCode, toString omitted
}</code>Equivalent record:
<code>public record Point(int x, int y) { }</code>Record with validation:
<code>public record Point(int x, int y) {
public Point {
if (x < 0) throw new IllegalArgumentException("Illegal argument");
}
}</code>Custom constructor and method in a record:
<code>public record Point(int x, int y) {
public Point(int age, String name) { this(0, 0); }
public void print() { System.out.printf("x: %d, y: %d%n", x, y); }
}</code>2.3 Sealed Classes
<code>public abstract sealed class Shape permits Circle { }
public final class Circle extends Shape { }</code>Sealed interface example:
<code>public sealed interface DAO permits PersonDAO, UserDAO { }
public final class PersonDAO implements DAO { }
public final class UserDAO implements DAO { }</code>2.4 Switch Expressions
<code>switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}
int a = 1;
String ret = switch (a) {
case 0 -> "zero";
case 1 -> "first";
default -> "default";
};
int b = 12;
String result = switch (b) {
case 0 -> "zero";
case 1 -> "first";
default -> {
String r = "default";
if (b > 10) r = "greater than 10";
yield r;
}
};</code>2.5 Text Blocks
<code>String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";</code>Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.