{"id":1150,"date":"2020-03-24T18:10:43","date_gmt":"2020-03-24T05:10:43","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1150"},"modified":"2021-09-28T14:03:53","modified_gmt":"2021-09-28T01:03:53","slug":"java-switch-expression","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1150","title":{"rendered":"Java Switch Expression"},"content":{"rendered":"<h1>Syntax<\/h1>\n<pre style=\"white-space: pre;\">switch(<strong><em>VARIABLE<\/em><\/strong>) {\r\n\tcase <strong><em>MATCH_1<\/em> <\/strong>[, <strong><em>MATCH_2<\/em><\/strong>][, <strong><em>MATCH_N<\/em><\/strong>] -&gt; <strong><em>EXPRESSION<\/em><\/strong>; | <strong><em>THROW-STATEMENT<\/em><\/strong>; | <strong><em>BLOCK<\/em><\/strong>\r\n\tdefault -&gt; <strong><em>EXPRESSION<\/em><\/strong>; | <strong><em>THROW-STATEMENT<\/em><\/strong>; | <strong><em>BLOCK<\/em><\/strong>\r\n}<\/pre>\n<table>\n<tbody>\n<tr>\n<td><strong>Token<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>VARIABLE<\/td>\n<td>The variable to test.<\/td>\n<\/tr>\n<tr>\n<td>MATCH_1, MATCH_2, MATCH_N<\/td>\n<td>The value or values that can match the VARIABLE.<\/td>\n<\/tr>\n<tr>\n<td>EXPRESSION<\/td>\n<td>A expression to execute.<\/td>\n<\/tr>\n<tr>\n<td>THROW-STATEMENT<\/td>\n<td>A expression that throws an exception.<\/td>\n<\/tr>\n<tr>\n<td>BLOCK<\/td>\n<td>A block of statements to execute.<\/p>\n<p>If a return value is required use the <strong>yield<\/strong> keyword instead of <strong>return<\/strong>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Example<\/h1>\n<pre style=\"white-space: pre;\">public enum Day { \r\n\tSUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; \r\n}\r\n\r\nDay day = Day.WEDNESDAY;\r\n\r\nint numLetters = switch (day) {\r\n    case MONDAY, FRIDAY, SUNDAY -&gt; 6;\r\n    case TUESDAY -&gt; 7;\r\n    case THURSDAY, SATURDAY -&gt; {\r\n        System.out.println(8);\r\n        yield 8;\r\n    }\r\n    case WEDNESDAY -&gt; {\r\n        System.out.println(9);\r\n        yield 9;\r\n    }\r\n    default -&gt; {\r\n        throw new IllegalStateException(\"Invalid day: \" + day);\r\n    }\r\n};\r\n\r\nSystem.out.println(numLetters);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Syntax switch(VARIABLE) { case MATCH_1 [, MATCH_2][, MATCH_N] -&gt; EXPRESSION; | THROW-STATEMENT; | BLOCK default -&gt; EXPRESSION; | THROW-STATEMENT; | BLOCK } Token Description VARIABLE The variable to test. MATCH_1, MATCH_2, MATCH_N The value or values that can match the VARIABLE. EXPRESSION A expression to execute. THROW-STATEMENT A expression that throws an exception. BLOCK A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1150"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1150"}],"version-history":[{"count":3,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1150\/revisions"}],"predecessor-version":[{"id":1153,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1150\/revisions\/1153"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}