{"id":3366,"date":"2025-12-24T10:00:58","date_gmt":"2025-12-24T15:00:58","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3366"},"modified":"2025-12-24T10:00:58","modified_gmt":"2025-12-24T15:00:58","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_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-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":3362,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-instanceof\/","url_meta":{"origin":3366,"position":0},"title":"Pattern Matching for instanceof","author":"Jeffery Miller","date":"December 24, 2025","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":3349,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/java-switch-expressions\/","url_meta":{"origin":3366,"position":1},"title":"Java Switch Expressions","author":"Jeffery Miller","date":"December 24, 2025","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":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":"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":1936,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/advancedstring-java-lang-string-steroids\/","url_meta":{"origin":3366,"position":3},"title":"AdvancedString &#8211; java.lang.String on steroids!","author":"Jeffery Miller","date":"November 24, 2025","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":[]},{"id":2526,"url":"https:\/\/www.mymiller.name\/wordpress\/pipelines\/pipeline-switches\/","url_meta":{"origin":3366,"position":4},"title":"Pipeline Switches","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"One of the things that make pipelines attract is the ability to create switches. Now my question to you is what if it isn't the data your processing that should determine if a switch pushes the data down an alternate pipeline? We have several methods that perform this task filter()\u2026","rel":"","context":"In &quot;Pipelines&quot;","block_context":{"text":"Pipelines","link":"https:\/\/www.mymiller.name\/wordpress\/category\/pipelines\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/railway-station-1270893_640.jpg?fit=640%2C384&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/railway-station-1270893_640.jpg?fit=640%2C384&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/03\/railway-station-1270893_640.jpg?fit=640%2C384&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1906,"url":"https:\/\/www.mymiller.name\/wordpress\/java_http\/http-server-command-handler-in-a-jar\/","url_meta":{"origin":3366,"position":5},"title":"HTTP Server Command Handler in a Jar","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"Today, I'm going to share with you a command handler for the HTTP Server I presented previously. \u00a0This is a handler to extend the abilities of that HTTP Server in a Jar to provide you the ability to stop, restart and get status of it, if needed. WARNING: This is\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":[]}],"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}]}}