Fundamentals 9 min read

Key New Features in Java 17: Text Blocks, Records, Switch Expressions, Pattern Matching, Sealed Classes, and Improved NullPointerException

The article reviews the most notable Java 17 language enhancements—including text blocks, records, concise switch expressions, pattern‑matching instanceof, sealed classes, and richer NullPointerException messages—explaining their syntax, benefits, and providing side‑by‑side code comparisons with Java 11.

Architecture Digest
Architecture Digest
Architecture Digest
Key New Features in Java 17: Text Blocks, Records, Switch Expressions, Pattern Matching, Sealed Classes, and Improved NullPointerException

Java 17 introduces several language changes that modernize Java code and simplify common patterns, while also improving performance and safety.

Text Blocks allow multi‑line string literals without cumbersome concatenation. For example, the same poem can be written in Java 11 as:

public String poem = "                " +
    "Twinkle, Twinkle, Little Star\n" +
    "                \n" +
    "                Twinkle, twinkle, little star,\n" +
    "How I wonder what you are!\n" +
    "Up above the world so high,\n" +
    "Like a diamond in the sky.\n" +
    "                \n" +
    "                Twinkle, twinkle, little star,\n" +
    "How I wonder what you are!";

In Java 17 the same code becomes much cleaner:

public String poem = """
                Twinkle, Twinkle, Little Star
                
                Twinkle, twinkle, little star,
                How I wonder what you are!
                Up above the world so high,
                Like a diamond in the sky.
                
                Twinkle, twinkle, little star,
                How I wonder what you are!
                """;

Records provide a compact syntax for immutable data carriers. The traditional Java 11 data class:

public class DataClass {
    public DataClass(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    private final Integer id;
    private final String name;
    public Integer getId() { return id; }
    public String getName() { return name; }
    @Override public boolean equals(Object o) { ... }
    @Override public int hashCode() { ... }
}

is reduced to a single line in Java 17:

public record DataClass(Integer id, String name) { }

Switch Expressions are now expressions that return a value and no longer require explicit break statements. The classic Java 11 version:

public String getSeasonDescription(Season season) {
    String seasonDescription;
    switch (season) {
        case SPRING:
            seasonDescription = "Flowers are blooming!";
            break;
        case SUMMER:
            seasonDescription = "It's hot outside!";
            break;
        case AUTUMN:
            seasonDescription = "Leaves are falling!";
            break;
        case WINTER:
            seasonDescription = "Snow is coming!";
            break;
        default:
            throw new IllegalStateException("Invalid season: " + season);
    }
    return seasonDescription;
}

becomes more concise in Java 17:

public String getSeasonDescription(Season season) {
    return switch (season) {
        case SPRING -> "Flowers are blooming!";
        case SUMMER -> "It's hot outside!";
        case AUTUMN -> "Leaves are falling!";
        case WINTER -> "Snow is coming!";
    };
}

Pattern‑Matching instanceof removes the need for a separate cast. Java 11 requires:

public void findInstance(Object o) {
    if (o instanceof String) {
        String s = (String) o;
        System.out.printf("Object is a string %s", s);
    } else if (o instanceof Number) {
        Number n = (Number) o;
        System.out.printf("Object is a number %n", n);
    }
}

Java 17 simplifies this to:

public void findInstance(Object o) {
    if (o instanceof String s) {
        System.out.printf("Object is a string %s", s);
    } else if (o instanceof Number n) {
        System.out.printf("Object is a number %n", n);
    }
}

Sealed Classes restrict which classes may extend a given class or implement an interface. An example sealed hierarchy:

public sealed class SealedClass permits AnotherSealedClass, AnotherNonSealedClass, AnotherFinalClass {}

Subclasses can be declared as non‑sealed , final , or further sealed :

public non-sealed class AnotherNonSealedClass extends SealedClass {}
public final class AnotherFinalClass extends SealedClass {}
public sealed class AnotherSealedClass extends SealedClass permits AnotherNonSealedClassTwo {}
public non-sealed class AnotherNonSealedClassTwo extends AnotherSealedClass {}

Improved NullPointerException messages now indicate the exact variable that was null. The classic Java 11 code:

public static void main(String... args) {
    String s = null;
    System.out.println(s.toLowerCase());
}

produces a generic stack trace. In Java 17 the same code yields a detailed message such as:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "s" is null
    at edu.javamodules.NpeExample.main(NpeExample.java:7)

These innovations collectively make Java 17 code more concise, expressive, and safer, encouraging developers to migrate from earlier LTS releases.

Conclusion – Java 17 delivers a compelling set of language features that reduce boilerplate, improve readability, and provide better diagnostics, while maintaining full backward compatibility, making it an attractive upgrade for any Java project.

nullpointerexceptionlanguage featuresSealed ClassesRecordsSwitch Expressionjava-17Text Blocks
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.