A logo showing the text blog.marcnuri.com
Español
Home»Java»Pattern Matching for instanceof in Java

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Pattern Matching for instanceof in Java

2022-03-12 in Java tagged Java / Java 17 by Marc Nuri | Last updated: 2025-01-25
Versión en Español

Introduction

Java, despite being a mature language, continues to evolve to embrace modern programming paradigms and best practices. Many of these enhancements focus on improving developer productivity and code readability.

One such feature was finally delivered in Java 16 and further improved in Java 17: JEP 394: Pattern Matching for instanceof.

This feature simplifies the writing of conditional logic involving type checks and casting, making the process more concise and expressive.

What is Pattern Matching?

Pattern matching is a programming construct that enables developers to test an object against a specific pattern. If the object matches the pattern, certain operations can be performed on it, such as extracting its properties or casting it to a specific type. This approach simplifies conditional logic by combining type checking, casting, and variable declaration into a single, concise operation.

Many programming languages, such as Scala, Kotlin, and Haskell, have implemented pattern matching as a core feature. With the introduction of JEP 394, Java has embraced this paradigm, offering developers a more modern and streamlined approach to type checking.

The problem with traditional instanceof

In traditional Java, checking an object's type requires two steps:

  1. Using the instanceof operator to verify the type.
  2. Explicitly casting the object to the desired type.

Here's an example:

TraditionalInstanceOf.java
public class TraditionalInstanceOf {
    public static void main(String[] args) {
        Object object = "Hello, World!";
        if (object instanceof String) {
            String string = (String) object; // Explicit cast is required
            System.out.println(string.toUpperCase());
        }
    }
}

While functional, this approach has a couple of drawbacks:

  1. Repetition: The variable object is referenced multiple times: once in the instanceof check and again during the cast.
  2. Verbosity: The explicit cast adds unnecessary boilerplate, reducing readability.

Pattern Matching for instanceof

JEP 394 eliminates the need for repetitive and redundant type checks by introducing a much cleaner and intuitive syntax. With this feature, you can combine type checking, casting, and variable declaration into a single operation.

Key advantages of pattern matching for instanceof include:

  1. Reduced Boilerplate: There is no longer a need to write the type name multiple times, which makes your code more concise and direct.
  2. Improved Readability: The code is easier to read and understand, as the type check and cast are combined into a single operation.
  3. Safer Code: Since the type check and cast are merged, there is no chance of mismatched types or forgetting to update a cast if the code around it changes.

The following code snippet demonstrates how to use pattern matching for instanceof:

PatternMatchingInstanceOf.java
public class PatternMatchingInstanceOf {
    public static void main(String[] args) {
        Object object = "Hello, World!";
        if (object instanceof String string) {
            System.out.println(string.toUpperCase());
        }
    }
}

In this version, we can see the following improvements:

  1. The instanceof operator not only checks the type but also declares and initializes a new variable (string) if the check passes.
  2. The variable string is automatically cast to the appropriate type (String) within the scope of the if block.

Conclusion

Pattern Matching for instanceof represents a significant leap toward more expressive and concise Java code. By reducing boilerplate and clarifying how variables are scoped to their conditional blocks, it brings a clean, modern feel to everyday type checking. JEP 394 finalizes this feature in Java 16, demonstrating Java’s ongoing commitment to improving developer productivity with each release.

References

  • JEP 394: Pattern Matching for instanceof
  • JEP 375: Pattern Matching for instanceof (Second Preview)
  • JEP 305: Pattern Matching for instanceof (Preview)
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Eclipse JKube 1.8.0 is now available!Eclipse JKube 1.7.0 is now available!
© 2007 - 2025 Marc Nuri