{"id":3366,"date":"2026-08-19T10:00:25","date_gmt":"2026-08-19T14:00:25","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3366"},"modified":"2026-08-19T10:00:25","modified_gmt":"2026-08-19T14:00:25","slug":"pattern-matching-for-switch","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-switch\/","title":{"rendered":"Pattern Matching for Switch"},"content":{"rendered":"<p>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, we will discuss how to use pattern matching for switch statements and provide examples of its usage.<\/p>\n<p>The basic syntax for a pattern matching switch statement is as follows:<\/p>\n<pre><code class=\"language-java\">switch (expression) {\n\tcase pattern1 -&gt; statement1;\n    case pattern2 -&gt; statement2;\n    \/\/ ...     case patternN -&gt; statementN;\n    default -&gt; statement; \n}\n<\/code><\/pre>\n<p>In this syntax, the expression is the value being tested, and each case represents a pattern to match against. The &#8220;-&gt;&#8221; symbol is used to separate the pattern from the statement that should be executed if the pattern matches. The &#8220;default&#8221; case is used to handle any values that do not match any of the specified patterns.<\/p>\n<p>Patterns can include type patterns, which test whether an expression is an instance of a specific type, and &#8220;instanceof&#8221; patterns, which test whether an expression is an instance of a type and bind the expression to a variable. Here&#8217;s an example of a pattern matching switch statement that uses both type and &#8220;instanceof&#8221; patterns:<\/p>\n<pre><code class=\"language-java\">public String getMessage(Object obj) {\n\treturn switch (obj) {\n\t\tcase String s -&gt; \"The string is: \" + s;\n\t\tcase Integer i &amp;&amp; i &gt; 0 -&gt; \"The integer is positive: \" + i;\n\t\tcase Integer i &amp;&amp; i &lt; 0 -&gt; \"The integer is negative: \" + i;         \n\t\tcase Date d -&gt; \"The date is: \" + d;\n\t\tdefault -&gt; \"Unknown object type\";     \n\t}; \n}\n<\/code><\/pre>\n<p>In this example, the &#8220;getMessage&#8221; method takes an object as input and returns a string based on the type of the input object. The first case uses a type pattern to test whether the input object is a string and binds the expression to the variable &#8220;s&#8221;. The second and third cases use &#8220;instanceof&#8221; patterns to test whether the input object is an integer and whether it is positive or negative, respectively. The fourth case uses a type pattern to test whether the input object is a date. The &#8220;default&#8221; case is used to handle any input objects that do not match any of the specified patterns.<\/p>\n<p>One of the benefits of pattern matching for switch statements is that it allows for more concise and readable code. Prior to this feature, developers often needed to use if-else statements or create separate methods to handle different cases, which could lead to more complex and verbose code. With pattern matching for switch statements, developers can more easily express their intentions and handle multiple cases in a single statement.<\/p>\n<p>In conclusion, pattern matching for switch statements is a new feature in Java 16 that allows for more concise and expressive switch statements. By using type and &#8220;instanceof&#8221; patterns, developers can more easily handle multiple cases in a single statement, leading to cleaner and more readable code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, we will discuss how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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},"jetpack_post_was_ever_published":false},"categories":[458],"tags":[],"series":[],"class_list":["post-3366","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\/network-cable-g9e6b10ea9_640.jpg?fit=640%2C427&ssl=1","jetpack-related-posts":[{"id":3349,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/java-switch-expressions\/","url_meta":{"origin":3366,"position":0},"title":"Java Switch Expressions","author":"Jeffery Miller","date":"August 19, 2026","format":false,"excerpt":"In Java, switch statements have traditionally been used to evaluate a value and execute different code blocks based on the value's match with specific cases. However, starting from Java 14, switch expressions were introduced as a preview feature, allowing for a more concise and expressive syntax. Along with switch expressions,\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\/sever-g8797be03e_640.jpg?fit=640%2C466&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/sever-g8797be03e_640.jpg?fit=640%2C466&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/sever-g8797be03e_640.jpg?fit=640%2C466&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3362,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-instanceof\/","url_meta":{"origin":3366,"position":1},"title":"Pattern Matching for instanceof","author":"Jeffery Miller","date":"August 19, 2026","format":false,"excerpt":"Java 16 introduced a new feature called \"Pattern Matching for instanceof\" 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,\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\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/jigsaw-gcae0977ae_640.png?fit=640%2C455&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":3366,"position":2},"title":"Record Patterns in Java 21: Simplifying Data Extraction","author":"Jeffery Miller","date":"August 19, 2026","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":4035,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/whats-new-in-jdk-26-features-jeps-and-enhancements\/","url_meta":{"origin":3366,"position":3},"title":"What&#8217;s New in JDK 26: Features, JEPs, and Enhancements","author":"Jeffery Miller","date":"September 18, 2026","format":false,"excerpt":"Released on March 17, 2026, JDK 26 is the six-month feature release following Java 25 (LTS). Bringing 10 official JDK Enhancement Proposals (JEPs) along with critical HotSpot runtime optimizations, JDK 26 introduces modern networking protocols, tightens language safety, optimizes garbage collection throughput, and refines concurrency patterns. Here is a comprehensive\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_rgp2zkrgp2zkrgp2-scaled.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_rgp2zkrgp2zkrgp2-scaled.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_rgp2zkrgp2zkrgp2-scaled.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_rgp2zkrgp2zkrgp2-scaled.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_rgp2zkrgp2zkrgp2-scaled.avif 3x"},"classes":[]},{"id":4038,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/whats-new-in-jdk-27-features-jeps-and-architectural-enhancements\/","url_meta":{"origin":3366,"position":4},"title":"What&#8217;s New in JDK 27: Features, JEPs, and Architectural Enhancements","author":"Jeffery Miller","date":"September 21, 2026","format":false,"excerpt":"Released on September 15, 2026, JDK 27 continues Java's six-month release cadence following JDK 26 and Java 25 (LTS). As the Reference Implementation of Java SE 27 under JSR 402, JDK 27 delivers nine official JDK Enhancement Proposals (JEPs) along with landmark performance and security advancements. Among the headline changes\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_lgbas2lgbas2lgba-1.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_lgbas2lgbas2lgba-1.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_lgbas2lgbas2lgba-1.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_lgbas2lgbas2lgba-1.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2026\/09\/Gemini_Generated_Image_lgbas2lgbas2lgba-1.avif 3x"},"classes":[]},{"id":1936,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/advancedstring-java-lang-string-steroids\/","url_meta":{"origin":3366,"position":5},"title":"AdvancedString &#8211; java.lang.String on steroids!","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Every need an additional method on the String Class? \u00a0Well I have and it would have made life much easier. \u00a0Unfortunately you can't subclass String as it is Final. \u00a0So what are you to do? \u00a0Well you wrap the String class. \u00a0I have created AdvancedString, which contains additional functionality and\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"AdvancedString","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&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\/3366","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=3366"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3366\/revisions"}],"predecessor-version":[{"id":3369,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3366\/revisions\/3369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3368"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3366"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}