Maciej Soliński
Back to the blog

From Java 7 to Java 25: less ceremony, more intent

Records, streams, var and pattern matching have changed everyday Java. What improves readability, and where should we exercise restraint?

From Java 7 to Java 25: less ceremony, more intent

Put the same program written in Java 7 and Java 25 side by side, and the difference is striking. One version has a data class, a constructor and loops. The other has a record, a pipeline and a switch that returns a result. You can see the change before reading the business logic.

Does that make it a completely different language? I find another question more useful: how much code do I need to read to understand what the program does?

One problem, two implementations

Start with a list of people. Select those named Bob, uppercase the name and print an age-band description. Assume valid input: non-negative ages and non-empty names. This is a syntax example, so input validation is omitted.

Here is a Java 7 compatible implementation in Java7Example.java:

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class Java7Example {
    static final class Person {
        final String name;
        final int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
            new Person("Alice", 30),
            new Person("Bob", 25),
            new Person("Charlie", 40),
            new Person("David", 18)
        );

        for (Person person : people) {
            if ("Bob".equals(person.name)) {
                Person renamed = new Person(
                    person.name.toUpperCase(Locale.ROOT), person.age);
                System.out.println(describe(renamed));
            }
        }
    }

    static String describe(Person person) {
        if (person.age < 20) {
            return person.name + ": under 20";
        }
        if (person.age < 40) {
            return person.name + ": 20-39";
        }
        return person.name + ": 40+";
    }
}

Java 7 does not require an intermediate result list for this task. A single straightforward loop already works. A useful comparison should give the older language a reasonable implementation.

With Java 25, we can write a compact source file named Java25Example.java:

record Person(String name, int age) {}

void main() {
    var people = List.of(
        new Person("Alice", 30),
        new Person("Bob", 25),
        new Person("Charlie", 40),
        new Person("David", 18)
    );

    people.stream()
        .filter(p -> "Bob".equals(p.name()))
        .map(p -> new Person(p.name().toUpperCase(Locale.ROOT), p.age()))
        .map(this::describe)
        .forEach(IO::println);
}

String describe(Person person) {
    return switch (person) {
        case Person(var name, var age) when age < 20 -> name + ": under 20";
        case Person(var name, var age) when age < 40 -> name + ": 20-39";
        case Person(var name, var age) -> name + ": 40+";
    };
}

The second example requires JDK 25 and uses no preview features:

java Java25Example.java

Both implementations are intended to print:

BOB: 20-39

Locale.ROOT makes the case conversion independent of the environment’s locale. That is a deliberate choice for identifier-style normalization; user-facing text may need language-specific rules instead.

Several releases, not one sudden revolution

A “7 versus 25” comparison spans years of development. The newer implementation combines features introduced across several releases [1–6, 10]:

Feature First release without preview
Lambdas, method references and Stream API Java 8
List.of(...) Java 9
Local-variable var Java 10
Records Java 16
Record patterns and pattern matching for switch Java 21
Compact source files and instance main methods Java 25

This matters when planning an upgrade. Most of the style shown here is already available in Java 21. In a regular class, retain the explicit class declaration, imports and an appropriate entry point.

Records define a data model

record Person(String name, int age) declares data components. The compiler supplies a constructor, accessors and implementations of equals, hashCode and toString, among other members [4].

That is a semantic difference too. The older class in this example does not implement value-based equality. The program prints the same result, but the two types are not interchangeable in every context.

A record also does not guarantee deep immutability. If a component holds a mutable list, that list can still change. The component reference is final; the entire object graph does not become immutable. Similarly, List.of prevents structural changes to the list without freezing its elements [4, 7].

My practical rule is to consider records for data passed between layers. The type’s contract should drive the choice, rather than the number of lines saved.

Streams and var: context determines readability

The pipeline makes its steps easy to scan: select, transform, describe, print. p -> ... is a lambda expression; this::describe is a method reference. Both work with functional interfaces [1, 2].

That does not automatically improve performance. Here, stream() creates a sequential stream. A loop remains a good choice, particularly when processing involves early exits, error handling or several side effects.

I take the same approach to var: it removes repetition when the initializer communicates the type clearly. The compiler still determines the type. The variable becomes neither dynamically typed nor automatically final [3].

Pattern matching still needs a readability check

The Person(var name, var age) pattern exposes the record components, while when adds a condition to a branch [5]. For this particular example, ordinary if statements may be just as readable. Demonstrating a language feature does not make it the best replacement for every conditional.

There is also a null-handling detail: without case null, passing null to this switch throws NullPointerException. New syntax does not remove the need for an explicit policy on missing data.

Less startup code, the same design decisions

A compact source file omits an explicit class declaration. A class still exists: the compiler declares it implicitly. These files automatically make types from the exported packages of java.base, including List and Locale, available without ordinary imports. IO.println simplifies console output [6].

That is useful for small programs and examples. Large applications still need named packages, module boundaries and a clear division of responsibilities.

Beyond syntax, virtual threads became permanent in Java 21, while the standard HTTP client arrived in Java 11. Virtual threads can improve throughput when many tasks wait for I/O; they do not make computation itself faster. The HTTP client supports both synchronous and asynchronous requests [8, 9].

For me, modern Java’s main benefit is the ability to express intent with less supporting code. During code review, the question I care about most is whether the next developer can understand the code sooner and change it safely.