Learning Note: Java 21 Feature – Unnamed Patterns and Variables

July 28, 2025 (4mo ago)

Jump to FAQs

As I continue my exploration of Java 21 with the “Java 21 – Exploring the Latest Innovations for 2024” course, I’ve reached Feature 3: unnamed patterns and variables. This enhancement, currently in preview, brings Java closer to modern programming practices by allowing developers to ignore unneeded data in pattern matching and method signatures.

Motivation: Ignore What You Don’t Need

The analogy presented in the course is organizing a large event with various ticket types (VIP, general admission, standing only). Sometimes, you only care whether a person can access a section—not all the details about their ticket. Similarly, in Java, you often find yourself forced to acknowledge variables you don’t actually use, cluttering your code with unnecessary details.

What’s New: Unnamed Patterns and Variables

With unnamed patterns and variables, you can now use underscores (_) to indicate that certain values are intentionally ignored. This is especially useful in pattern matching, such as when using instanceof or record deconstruction, where you might only need a subset of the available data.

Example

Suppose you have a record like this:

record Ticket(String type, String holderName, int seatNumber) {}
 
void checkAccess(Object ticket) {
    if (ticket instanceof Ticket(String type, String _, int _)) {
        if ("VIP".equals(type)) {
            System.out.println("Access to all areas.");
        }
    }
}

In this example:

This makes the code cleaner and more focused, allowing you to express your intent without unnecessary boilerplate.

Key Advantages

Takeaway

Unnamed patterns and variables in Java 21 help you write more concise, focused, and modern Java code. By letting you ignore what you don’t need, your logic becomes clearer and your programs become easier to maintain—an important step forward for Java’s evolution.

Discuss this post:

Frequently Asked Questions

What are unnamed patterns and variables in Java 21?

Unnamed patterns and variables allow you to use underscores (_) to explicitly ignore values you don't need in pattern matching and variable declarations. This makes code cleaner by showing intent to ignore certain data rather than creating unused variables.

Why would I want to ignore variables instead of just not using them?

Using unnamed patterns prevents compiler warnings about unused variables and makes your code more expressive. It clearly communicates to other developers (and your future self) that you're intentionally ignoring certain values, not just forgetting to use them.

Do I need to enable preview features to use unnamed patterns?

Yes, unnamed patterns and variables are currently a preview feature in Java 21. You need to compile and run your code with the --enable-preview flag to use this feature.

Can I use multiple underscores in the same pattern?

Yes, you can use multiple underscores in the same pattern to ignore multiple values. Each underscore represents a separate ignored value, as shown in the Ticket example where both holderName and seatNumber are ignored.

Where can I use unnamed patterns besides instanceof checks?

You can use unnamed patterns in various contexts including record deconstruction, switch expressions with pattern matching, catch blocks for exceptions you don't need to reference, and lambda parameters you don't use.

Is using underscore for unnamed variables a new concept in programming?

No, many modern programming languages like Scala, Kotlin, Python, and Go use underscores to indicate intentionally unused variables. Java 21 is adopting this widely-accepted convention.

Will this feature replace the need for unused variable suppressions?

In many cases, yes. Instead of using @SuppressWarnings("unused") annotations, you can use unnamed patterns to explicitly show that certain values are intentionally ignored, making the code more self-documenting.

Can I still access a value after marking it as unnamed?

No, once you use an underscore to mark a value as unnamed, that value is not bound to any variable and cannot be accessed later in your code. This is intentional to enforce the 'ignore' semantics.