{"id":3362,"date":"2025-12-24T10:00:54","date_gmt":"2025-12-24T15:00:54","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3362"},"modified":"2025-12-24T10:00:54","modified_gmt":"2025-12-24T15:00:54","slug":"pattern-matching-for-instanceof","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-instanceof\/","title":{"rendered":"Pattern Matching for instanceof"},"content":{"rendered":"\n<p>Java 16 introduced a new feature called &#8220;Pattern Matching for instanceof&#8221; which simplifies the process of casting objects in Java. This feature improves the readability and safety of code that uses <code>instanceof<\/code> and casting by allowing developers to combine the two operations into a single step.<\/p>\n\n\n\n<p>In this blog article, we will explore what Pattern Matching for instanceof is, how it works, and how to use it in your Java applications.<\/p>\n\n\n\n<p>What is Pattern Matching for instanceof?<\/p>\n\n\n\n<p>Pattern Matching for instanceof is a new feature in Java 16 that improves the syntax and safety of casting in Java. It allows developers to combine the operations of <code>instanceof<\/code> and casting into a single step, making code more concise and readable.<\/p>\n\n\n\n<p>The feature achieves this by introducing a new syntax that allows us to declare a variable as part of an <code>instanceof<\/code> check. This variable can then be used in the code block that follows the check, eliminating the need for an additional cast.<\/p>\n\n\n\n<p>How Pattern Matching for instanceof Works<\/p>\n\n\n\n<p>Pattern Matching for instanceof works by allowing us to declare a variable as part of an <code>instanceof<\/code> check. The syntax for this is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (object instanceof ClassName variableName) {     \n\t\/\/ Use variableName as a variable of type ClassName in this block \n}\n<\/code><\/pre>\n\n\n\n<p>In this syntax, <code>object<\/code> is the object that we are checking, <code>ClassName<\/code> is the class that we are checking against, and <code>variableName<\/code> is the name of the variable that we want to create if the check passes.<\/p>\n\n\n\n<p>If the <code>instanceof<\/code> check passes, the variable <code>variableName<\/code> is created and can be used in the code block that follows. This variable has a type of <code>ClassName<\/code>, so there is no need to cast it.<\/p>\n\n\n\n<p>Example Usage<\/p>\n\n\n\n<p>Let&#8217;s take a look at an example to see how Pattern Matching for instanceof works in practice. Suppose we have an interface called <code>Shape<\/code> and two classes that implement it: <code>Circle<\/code> and <code>Rectangle<\/code>. We want to write a method that returns the area of a shape, but we don&#8217;t know which type of shape we are dealing with.<\/p>\n\n\n\n<p>We can use Pattern Matching for instanceof to simplify this method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public double getArea(Shape shape) {\n\tif (shape instanceof Circle circle) {\n\t         return Math.PI * circle.getRadius() * circle.getRadius();     \n\t} else if (shape instanceof Rectangle rectangle) {\n\t         return rectangle.getWidth() * rectangle.getHeight();     \n\t} else {\n\t          throw new IllegalArgumentException(\"Unknown shape type\");     \n\t} \n}\n<\/code><\/pre>\n\n\n\n<p>In this example, we use Pattern Matching for instanceof to check if the <code>shape<\/code> parameter is an instance of <code>Circle<\/code> or <code>Rectangle<\/code>. If it is, we create a variable (<code>circle<\/code> or <code>rectangle<\/code>) of the appropriate type, which can be used in the code block that follows.<\/p>\n\n\n\n<p>We can then use these variables to calculate the area of the shape. If the shape is neither a circle nor a rectangle, we throw an exception with an error message.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>Pattern Matching for instanceof is a new feature in Java 16 that simplifies the process of casting in Java. By allowing us to combine the operations of <code>instanceof<\/code> and casting into a single step, it makes code more concise and readable. The feature is especially useful in situations where we need to deal with multiple types of objects, such as when working with interfaces or abstract classes. With Pattern Matching for instanceof, we can improve the safety and readability of our code, making it easier to understand and maintain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 16 introduced a new feature called &#8220;Pattern Matching for instanceof&#8221; which simplifies the process of casting objects in Java. This feature improves the readability and safety of code that uses instanceof and casting by allowing developers to combine the two operations into a single step. In this blog article, we will explore what Pattern [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[458],"tags":[],"series":[],"class_list":["post-3362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_new_features"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1","jetpack-related-posts":[{"id":3366,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-switch\/","url_meta":{"origin":3362,"position":0},"title":"Pattern Matching for Switch","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Pattern matching for switch statements is a new feature introduced in Java 16 that allows for more concise and expressive switch statements. This feature enables developers to use pattern matching to test expressions against multiple patterns, making it easier to write cleaner and more readable code. In this blog article,\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/network-cable-g9e6b10ea9_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/network-cable-g9e6b10ea9_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/network-cable-g9e6b10ea9_640.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3600,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/record-patterns-in-java-21-simplifying-data-extraction\/","url_meta":{"origin":3362,"position":1},"title":"Record Patterns in Java 21: Simplifying Data Extraction","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 21 introduces record patterns, a powerful addition to the pattern matching arsenal. This feature streamlines the extraction of components from record classes, making code more concise and readable. What are Record Patterns? Record patterns leverage the structure of record classes to deconstruct them into their constituent parts. By specifying\u2026","rel":"","context":"In &quot;Java New Features&quot;","block_context":{"text":"Java New Features","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_new_features\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/07\/collector-3930337_1280-jpg.avif 3x"},"classes":[]},{"id":3764,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/mastering-fallback-methods-in-spring-cloud-circuit-breaker\/","url_meta":{"origin":3362,"position":2},"title":"Mastering Fallback Methods in Spring Cloud Circuit Breaker","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides an elegant way to handle failures in distributed systems by employing the circuit breaker pattern. A crucial aspect of this pattern is the fallback mechanism, which allows your application to gracefully handle situations where a service dependency is unavailable or experiencing issues. This article dives\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 3x"},"classes":[]},{"id":3884,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_messaging\/mastering-polymorphic-data-in-spring-kafka-with-avro-union-types\/","url_meta":{"origin":3362,"position":3},"title":"Mastering Polymorphic Data in Spring Kafka with Avro Union Types","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"As a software architect, designing robust, scalable, and adaptable distributed systems is a constant pursuit. When working with Apache Kafka, a common challenge arises: how do you send messages that, while adhering to a generic wrapper, can carry different types of payloads based on the specific event or context? Imagine\u2026","rel":"","context":"In &quot;Spring Messaging&quot;","block_context":{"text":"Spring Messaging","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_messaging\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/plastic-5527530_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/plastic-5527530_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/plastic-5527530_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/plastic-5527530_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/plastic-5527530_1280.avif 3x"},"classes":[]},{"id":1932,"url":"https:\/\/www.mymiller.name\/wordpress\/java_http\/http-server-filters-jar\/","url_meta":{"origin":3362,"position":4},"title":"HTTP Server Filters in a Jar","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"We discussed the HTTP Server ContextHandler in a Jar\u00a0one of the methods was ContextHandlerInterface.getFilters() this method returns a list of Filters to apply to incoming HTTP Requests. \u00a0We subclass\u00a0com.sun.net.httpserver.Filter for these classes. These can be used to do processing on the request and add attributes to the HTTP Exchange. Filters\u2026","rel":"","context":"In &quot;Java HTTP&quot;","block_context":{"text":"Java HTTP","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_http\/"},"img":{"alt_text":"filters","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3008,"url":"https:\/\/www.mymiller.name\/wordpress\/springboot\/spring-annotation-w-dynamic-service-loading\/","url_meta":{"origin":3362,"position":5},"title":"Spring Annotation w\/Dynamic Service Loading","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Recently a project I was working on required the ability to load up a number of unknown services that implemented a Calendar Provider Interface. As this was a Spring server, I started looking around trying to find the right way to do this. I began by looking at being able\u2026","rel":"","context":"In &quot;Springboot&quot;","block_context":{"text":"Springboot","link":"https:\/\/www.mymiller.name\/wordpress\/category\/springboot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/03\/computer-1343393_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/03\/computer-1343393_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/03\/computer-1343393_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3362"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3362\/revisions"}],"predecessor-version":[{"id":3365,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3362\/revisions\/3365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3364"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3362"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}