{"id":6491,"date":"2024-04-21T23:29:02","date_gmt":"2024-04-22T06:29:02","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-switch\/"},"modified":"2024-04-21T23:29:47","modified_gmt":"2024-04-22T06:29:47","slug":"java-switch","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-switch\/","title":{"rendered":"Java switch"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Java switch statement to compare a value with multiple values and execute different code based on matches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Java switch statement<\/h2>\n\n\n\n<p>The <code>switch<\/code> statement allows you to evaluate an <code>expression<\/code>, compare its result with multiple possible values, and execute different code blocks based on the match.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the switch statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">switch<\/span> (expression) {\n    <span class=\"hljs-keyword\">case<\/span> value1:\n        <span class=\"hljs-comment\">\/\/ Code to execute when expression equals value1<\/span>\n        <span class=\"hljs-keyword\">break<\/span>;\n    <span class=\"hljs-keyword\">case<\/span> value2:\n        <span class=\"hljs-comment\">\/\/ Code to execute when expression equals value2<\/span>\n        <span class=\"hljs-keyword\">break<\/span>;\n    <span class=\"hljs-comment\">\/\/ More cases...<\/span>\n    <span class=\"hljs-keyword\">default<\/span>:\n        <span class=\"hljs-comment\">\/\/ Code to execute when none of the cases match the expression<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>expression<\/code>: This is the expression whose value will be compared against the <code>case<\/code> values.<\/li>\n\n\n\n<li><code>value1<\/code>, <code>value2<\/code>, etc.: These are the possible values that the <code>expression<\/code> can match. You can have multiple <code>case<\/code> labels.<\/li>\n\n\n\n<li><code>break<\/code>: The <code>break<\/code> statement is used to exit the <code>switch<\/code> block once a match is found. Without <code>break<\/code>, the control will continue to the next <code>case<\/code>, which may lead to unexpected behavior.<\/li>\n<\/ul>\n\n\n\n<p>How the switch statement works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, evaluate the <code>expression<\/code>. <\/li>\n\n\n\n<li>Second, compare the results of the <code>expression<\/code> with each <code>case<\/code> label.<\/li>\n\n\n\n<li>Third, execute the code block associated with that <code>case<\/code> label if a match is found.<\/li>\n\n\n\n<li>Finally, if no match is found, and there is a <code>default<\/code> case, execute the code block under the <code>default<\/code> label.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">expression<\/h3>\n\n\n\n<p>This is the expression that the switch statement will evaluate and use its result to compare against multiple values.<\/p>\n\n\n\n<p>The switch works with the following primitive types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>byte<\/li>\n\n\n\n<li>sort<\/li>\n\n\n\n<li>int<\/li>\n\n\n\n<li>char<\/li>\n<\/ul>\n\n\n\n<p>Besides these primitive types, the switch also supports the wrapper types of these primitive types, the enum type, and the String class. You&#8217;ll learn more about these types later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">case label<\/h3>\n\n\n\n<p>The switch statement may contain one or more case labels:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">case<\/span> value:<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the case label, you specify the value that the expression is compared against. The value in the case label must be a compile-time constant. In other words, it must be a literal or final variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">default case<\/h3>\n\n\n\n<p>The default is optional in a switch statement. The switch statement will execute the default case when the expression doesn&#8217;t match any values. <\/p>\n\n\n\n<p>It&#8217;s a good practice to always include a default case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">break statement<\/h3>\n\n\n\n<p>The <code>break<\/code> statement is used to exit the <code>switch<\/code> block once a match is found. <\/p>\n\n\n\n<p>Without <code>break<\/code>, the control will &#8220;fall through&#8221; to the next <code>case<\/code>, executing all subsequent code blocks until a <code>break<\/code> is encountered or the <code>switch<\/code> block ends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java switch statement examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the Java switch statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple Java switch example<\/h3>\n\n\n\n<p>The following example illustrates how to use the switch statement to display the day name based on the day number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> day = <span class=\"hljs-number\">3<\/span>;\n\n        <span class=\"hljs-keyword\">switch<\/span> (day) {\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">1<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Sunday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">2<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Monday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">3<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Tuesday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">4<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Wednesday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">5<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Thursday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">6<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Friday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">7<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Saturday\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">default<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Invalid day\"<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Tuesday<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare the day variable and initialize its value to 3.<\/p>\n\n\n\n<p>Second, compare the day with values from 1 to 7 and display the day&#8217;s names respectively. If the day is not between 1 and 7, display the message &#8220;Invalid day&#8221; in the default.<\/p>\n\n\n\n<p>The program displays the message &#8220;Tuesday&#8221; because the day is 3 which matches case 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using switch statement with fall through example<\/h3>\n\n\n\n<p>The following example illustrates how to use the switch statement to display quarter based on the provided month:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span> <\/span>{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">byte<\/span> month = <span class=\"hljs-number\">5<\/span>;\n        <span class=\"hljs-keyword\">switch<\/span> (month) {\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">1<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">2<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">3<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Quarter 1\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">4<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">5<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">6<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Quarter 2\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">7<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">8<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">9<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Quarter 3\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">10<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">11<\/span>:\n            <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-number\">12<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Quarter 4\"<\/span>);\n                <span class=\"hljs-keyword\">break<\/span>;\n            <span class=\"hljs-keyword\">default<\/span>:\n                System.out.println(<span class=\"hljs-string\">\"Invalid month\"<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">Quarter <span class=\"hljs-number\">2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The program will print:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Quarter 1 if the month is 1, 2, and 3. <\/li>\n\n\n\n<li>Quarter 2 if the month is 4, 5, and 6.<\/li>\n\n\n\n<li>Quarter 3 if the month is 7, 8, and 9.<\/li>\n\n\n\n<li>Quarter 4 if the month is 10, 11, and 12.<\/li>\n<\/ul>\n\n\n\n<p>In this program, you can see that <code>case<\/code> blocks 1, 2, and 3, for example, do not have <code>break<\/code> statements. This means that if <code>month<\/code> is 1, the code under &#8220;Quarter 1&#8221; is executed, and then it continues to execute the code under &#8220;Quarter 2&#8221; and &#8220;Quarter 3&#8221; as well. This behavior continues until a <code>break<\/code> statement is encountered or until the <code>switch<\/code> block ends.<\/p>\n\n\n\n<p>As you can see, we intentionally use &#8220;fall through&#8221; to group multiple months into the same quarter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java switch statement to compare a value with multiple values and execute different blocks of code based on matches.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Java switch statement to compare a value with multiple values and execute different code based on matches.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6491","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6491"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6491\/revisions"}],"predecessor-version":[{"id":6493,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6491\/revisions\/6493"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}