{"id":3349,"date":"2025-12-24T10:00:45","date_gmt":"2025-12-24T15:00:45","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3349"},"modified":"2025-12-24T10:00:45","modified_gmt":"2025-12-24T15:00:45","slug":"java-switch-expressions","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/java-switch-expressions\/","title":{"rendered":"Java Switch Expressions"},"content":{"rendered":"\n<p>In Java, switch statements have traditionally been used to evaluate a value and execute different code blocks based on the value&#8217;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, the new <code>yield<\/code> keyword was introduced to return a value from a switch case.<\/p>\n\n\n\n<p>Switch Expressions:<\/p>\n\n\n\n<p>Switch expressions in Java provide a more streamlined syntax compared to traditional switch statements. They are expressions, meaning they can produce a value, and they can be used in assignments, return statements, and method parameters.<\/p>\n\n\n\n<p>The syntax of a switch expression is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result = switch (expression) {\n    case value1 -&gt; expression1;\n    case value2 -&gt; expression2;\n    case value3 -&gt; expression3;\n    ...\n    default -&gt; expressionDefault;\n};\n<\/code><\/pre>\n\n\n\n<p>Key points to note about switch expressions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <code>expression<\/code> is evaluated, and the control flow matches the first case that matches the expression&#8217;s value.<\/li>\n\n\n\n<li>Each case is followed by <code>-&gt;<\/code> and an expression or a block of code.<\/li>\n\n\n\n<li>The <code>default<\/code> case is optional and executed when none of the previous cases match.<\/li>\n\n\n\n<li>The <code>result<\/code> variable stores the value produced by the executed case.<\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s look at an example to understand switch expressions better:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int day = 3;\nString dayName = switch (day) {\n    case 1 -&gt; \"Monday\";\n    case 2 -&gt; \"Tuesday\";\n    case 3 -&gt; \"Wednesday\";\n    case 4 -&gt; \"Thursday\";\n    case 5 -&gt; \"Friday\";\n    case 6, 7 -&gt; {\n        System.out.println(\"It's the weekend!\");\n        yield \"Weekend\";\n    }\n    default -&gt; throw new IllegalArgumentException(\"Invalid day: \" + day);\n};\n\nSystem.out.println(\"Today is \" + dayName);\n<\/code><\/pre>\n\n\n\n<p>In this example, based on the value of the <code>day<\/code> variable, the switch expression matches the corresponding case and assigns the result to the <code>dayName<\/code> variable. If the value is 6 or 7 (representing the weekend), a block of code is executed, printing a message and using the <code>yield<\/code> keyword to return the value &#8220;Weekend&#8221;. Finally, the value of <code>dayName<\/code> is printed.<\/p>\n\n\n\n<p>The Yield Keyword:<\/p>\n\n\n\n<p>The <code>yield<\/code> keyword is used to return a value from a switch expression. It can only be used within a block of code and is associated with a particular case. The <code>yield<\/code> keyword allows you to both specify a value and terminate the switch expression.<\/p>\n\n\n\n<p>In the previous example, the <code>yield<\/code> keyword is used to return the value &#8220;Weekend&#8221; when the case matches 6 or 7. Without the <code>yield<\/code> keyword, the block of code would execute, but the switch expression would continue evaluating other cases.<\/p>\n\n\n\n<p>Conclusion:<\/p>\n\n\n\n<p>Switch expressions, along with the <code>yield<\/code> keyword, bring enhanced functionality and readability to switch statements in Java. They allow developers to write more concise code and use switch statements as expressions that produce values. By embracing switch expressions and the <code>yield<\/code> keyword, developers can take advantage of a more expressive and streamlined syntax for handling multi-way branching in their Java programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, switch statements have traditionally been used to evaluate a value and execute different code blocks based on the value&#8217;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, the new yield keyword was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3351,"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-3349","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\/sever-g8797be03e_640.jpg?fit=640%2C466&ssl=1","jetpack-related-posts":[{"id":3366,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-switch\/","url_meta":{"origin":3349,"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":3349,"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":3727,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-expression-language-spel-dynamic-power-for-your-applications\/","url_meta":{"origin":3349,"position":2},"title":"Spring Expression Language (SpEL): Dynamic Power for Your Applications","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Spring Expression Language (SpEL) is a powerful tool in the Spring framework that allows you to write expressions that can be evaluated dynamically at runtime. Think of it as a mini-language within your Java code that lets you access and manipulate objects, call methods, and perform operations on data in\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ask-2110967_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ask-2110967_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ask-2110967_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ask-2110967_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/ask-2110967_1280-jpg.avif 3x"},"classes":[]},{"id":2526,"url":"https:\/\/www.mymiller.name\/wordpress\/pipelines\/pipeline-switches\/","url_meta":{"origin":3349,"position":3},"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":3362,"url":"https:\/\/www.mymiller.name\/wordpress\/java_new_features\/pattern-matching-for-instanceof\/","url_meta":{"origin":3349,"position":4},"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":1906,"url":"https:\/\/www.mymiller.name\/wordpress\/java_http\/http-server-command-handler-in-a-jar\/","url_meta":{"origin":3349,"position":5},"title":"HTTP Server Command Handler in a Jar","author":"Jeffery Miller","date":"April 20, 2026","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\/3349","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=3349"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3349\/revisions"}],"predecessor-version":[{"id":3352,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3349\/revisions\/3352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3351"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3349"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}