{"id":5218,"date":"2021-03-23T12:43:40","date_gmt":"2021-03-23T12:43:40","guid":{"rendered":"https:\/\/ded9.com\/?p=5218"},"modified":"2025-10-29T08:10:45","modified_gmt":"2025-10-29T08:10:45","slug":"learn-how-to-switch-in-java-in-very-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learn-how-to-switch-in-java-in-very-simple-language\/","title":{"rendered":"Learn the Java Switch Statement in the Simplest Way Possible"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn to use the switch Command to control the flow of your program. In <a href=\"https:\/\/ded9.com\/30-tips-on-java-programming-and-best-practices-for-beginners\/\">Java<\/a>, the if..else..if ladder executes code from many blocks.\u00a0<\/span><\/p>\n<p>The switch Command can replace long ladders that make the code legible.<span id=\"more-50073\"><\/span><\/p>\n<p>The syntax of the<a href=\"https:\/\/www.w3schools.com\/java\/java_switch.asp\" target=\"_blank\" rel=\"noopener\"> switch Command<\/a> is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">switch (variable \/ expression) {\r\n\r\ncase value1:\r\n\r\n\/\/ statements\r\n\r\nbreak;\r\n\r\ncase value2:\r\n\r\n\/\/ statements\r\n\r\nbreak;\r\n\r\n.. ..\u2026\r\n\r\n.. ..\u2026\r\n\r\ndefault:\r\n\r\n\/\/ statements\r\n\r\n}<\/pre>\n<\/div>\n<p>The switch Command evaluates the expression (mostly variable) and compares it with each label&#8217;s values (can be an expression).<\/p>\n<p>The switch Command executes all commands of the matching label.<\/p>\n<p>Suppose the variable\/expression is equal to value2. In this case, all commands related to this adaptive case are executed.<\/p>\n<p><strong>Note:<\/strong> Use the phrase break to terminate the execution of the switch statement. Break statements are important because if not used, all commands after the matching tag will be executed in sequence until the end of the switch statement.<\/p>\n<h2><span style=\"font-size: 18pt;\">Flowchart Command switch<\/span><\/h2>\n<h2><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258037 size-full\" title=\"Switch In Java\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart.png\" alt=\"Flowchart Command switch\" width=\"1680\" height=\"2144\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart.png 1680w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart-235x300.png 235w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart-802x1024.png 802w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart-768x980.png 768w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart-1204x1536.png 1204w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/624bffae64d6b60b5e49105e_Switch-Case-Flowchart-1605x2048.png 1605w\" sizes=\"(max-width: 1680px) 100vw, 1680px\" \/><\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>It should also be noted that the switch Command in Java only works with the following:<\/p>\n<ul>\n<li>Initial data types: byte, short, char, and int<\/li>\n<li>Types (enum (enum Java<\/li>\n<li>String class<\/li>\n<li>Character, Byte, Short, and Integer classes.<\/li>\n<\/ul>\n<p><strong>Example 1: Switch Command in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Day {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int week = 4;<\/li>\n<li dir=\"ltr\">String day;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">switch (week) {<\/li>\n<li dir=\"ltr\">case 1:<\/li>\n<li dir=\"ltr\">day = \u201cSunday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 2:<\/li>\n<li dir=\"ltr\">day = \u201cMonday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 3:<\/li>\n<li dir=\"ltr\">day = \u201cTuesday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 4:<\/li>\n<li dir=\"ltr\">day = \u201cWednesday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 5:<\/li>\n<li dir=\"ltr\">day = \u201cThursday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 6:<\/li>\n<li dir=\"ltr\">day = \u201cFriday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case 7:<\/li>\n<li dir=\"ltr\">day = \u201cSaturday\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">default:<\/li>\n<li dir=\"ltr\">day = \u201cInvalid day\u201d;<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (day);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Wednesday<\/p>\n<\/blockquote>\n<p><strong>Example 2: Switch Command in Java<\/strong><\/p>\n<p>The following program receives three inputs from the user: an operator and two numbers. It then performs calculations based on the numbers and operators entered. The result is then displayed.<\/p>\n<p>We used the scanner object to get input from the user.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Scanner;<\/li>\n<li dir=\"ltr\">class Calculator {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">char operator;<\/li>\n<li dir=\"ltr\">Double number1, number2, result;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">Scanner scanner = new Scanner (System.in);<\/li>\n<li dir=\"ltr\">System.out.print (&#8220;Enter operator (either +, -, * or \/):&#8221;);<\/li>\n<li dir=\"ltr\">operator = scanner.next (). charAt (0);<\/li>\n<li dir=\"ltr\">System.out.print (&#8220;Enter number1 and number2 respectively:&#8221;);<\/li>\n<li dir=\"ltr\">number1 = scanner.nextDouble ();<\/li>\n<li dir=\"ltr\">number2 = scanner.nextDouble ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">switch (operator) {<\/li>\n<li dir=\"ltr\">case &#8216;+&#8217;:<\/li>\n<li dir=\"ltr\">result = number1 + number2;<\/li>\n<li dir=\"ltr\">System.out.print (number1 + \u201c+\u201d + number2 + \u201d=\u201d + result);<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case &#8216;-&#8216;:<\/li>\n<li dir=\"ltr\">result = number1 &#8211; number2;<\/li>\n<li dir=\"ltr\">System.out.print (number1 + \u201c-\u201d + number2 + \u201d=\u201d + result);<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case &#8216;*&#8217;:<\/li>\n<li dir=\"ltr\">result = number1 * number2;<\/li>\n<li dir=\"ltr\">System.out.print (number1 + \u201c*\u201d + number2 + \u201d=\u201d + result);<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">case &#8216;\/&#8217;:<\/li>\n<li dir=\"ltr\">result = number1 \/ number2;<\/li>\n<li dir=\"ltr\">System.out.print (number1 + \u201c\/\u201d + number2 + \u201d=\u201d + result);<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">default:<\/li>\n<li dir=\"ltr\">System.out.println (\u201cInvalid operator!\u201d);<\/li>\n<li dir=\"ltr\">break;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Enter operator (either +, -, * or \/): *<\/p>\n<p dir=\"ltr\">Enter number1 and number2 respectively: 1.4<\/p>\n<p dir=\"ltr\">-5.3<\/p>\n<p dir=\"ltr\">1.4 * -5.3 = -7.419999999999999<\/p>\n<\/blockquote>\n<h1 data-sourcepos=\"1:1-1:33\"><span style=\"font-size: 18pt;\"><strong>Language Introduction History<\/strong><\/span><\/h1>\n<p data-sourcepos=\"3:1-3:367\">This mechanism was introduced in Java 12 (JEP 325) as a preview feature. Subsequently, a second preview was presented in Java 13 (JEP 354), and the final functionality was introduced in Java 14 (JEP 361) without any changes. Switch Expressions are part of Project Amber, which also anticipates introducing other practical changes to Java.<\/p>\n<p data-sourcepos=\"5:1-5:74\">Interestingly, in Java 12, the keyword <code>break<\/code> was used to return a value:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">String season = \"Winter\";\r\nString translation = switch (season) {\r\ncase \"Spring\":\r\nbreak \"wiosna\";\r\ncase \"Summer\":\r\nbreak \"lato\";\r\ncase \"Autumn\":\r\nbreak \"jesie\u0144\";\r\ncase \"Winter\":\r\nbreak \"zima\";\r\ndefault:\r\nbreak \"nieznany\";\r\n};<\/pre>\n<\/div>\n<p data-sourcepos=\"5:1-5:74\">The exact keyword had always been used to exit a <code>case<\/code> block. To differentiate between these two uses, starting from Java 13, the keyword <code>yield<\/code> must be used when returning a value.<\/p>\n<h1 data-sourcepos=\"1:1-1:37\"><span style=\"font-size: 18pt;\"><strong>Further Development of the Switch<\/strong><\/span><\/h1>\n<h1 data-sourcepos=\"1:1-1:37\"><span style=\"font-size: 18pt;\"><strong><img decoding=\"async\" class=\"aligncenter wp-image-258041 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/67600ae163224_switch_statement_in_java.jpg\" alt=\"Further Development of the Switch\" width=\"1200\" height=\"800\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/67600ae163224_switch_statement_in_java.jpg 1200w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/67600ae163224_switch_statement_in_java-300x200.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/67600ae163224_switch_statement_in_java-1024x683.jpg 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/67600ae163224_switch_statement_in_java-768x512.jpg 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/strong><\/span><\/h1>\n<p data-sourcepos=\"3:1-3:220\">It can be said that Switch Expression is the first step in the evolution of the <code>switch<\/code> Statement. The next stage involves work on Pattern Matching for the final version, which is not expected before JDK 20.<\/p>\n<p><iframe title=\"switch Statement in Java\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/wcwWlasmLWs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p data-sourcepos=\"5:1-5:43\"><strong>Before the Switch Expression was introduced<\/strong><\/p>\n<p data-sourcepos=\"7:1-7:98\">Before the introduction of this mechanism, we had to handle such scenarios in the following way:<\/p>\n<div class=\"code-block ng-tns-c4269412455-34 ng-trigger ng-trigger-codeBlockRevealAnimation\">\n<div class=\"code-block-decoration header-formatted gds-title-s ng-tns-c4269412455-34 ng-star-inserted\">\n<p><span class=\"ng-tns-c4269412455-34\">Java<\/span><\/p>\n<div class=\"buttons ng-tns-c4269412455-34 ng-star-inserted\"><\/div>\n<\/div>\n<div class=\"formatted-code-block-internal-container ng-tns-c4269412455-34\">\n<div class=\"animated-opacity ng-tns-c4269412455-34\">\n<pre class=\"ng-tns-c4269412455-34\"><code class=\"code-container formatted ng-tns-c4269412455-34\" role=\"text\" data-test-id=\"code-content\" data-sourcepos=\"9:1-27:1\">String season = <span class=\"hljs-string\">\"Winter\"<\/span>;\r\n<span class=\"hljs-keyword\">switch<\/span> (season) {\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Spring\"<\/span>:\r\n        System.out.println(<span class=\"hljs-string\">\"Mamy aktualnie wiosn\u0119\"<\/span>);\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Summer\"<\/span>:\r\n        System.out.println(<span class=\"hljs-string\">\"Mamy aktualnie lato\"<\/span>);\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Autumn\"<\/span>:\r\n        System.out.println(<span class=\"hljs-string\">\"Mamy aktualnie jesie\u0144\"<\/span>);\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n    <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Winter\"<\/span>:\r\n        System.out.println(<span class=\"hljs-string\">\"Mamy aktualnie zim\u0119\"<\/span>);\r\n        <span class=\"hljs-keyword\">break<\/span>;\r\n    <span class=\"hljs-keyword\">default<\/span>:\r\n        System.out.println(<span class=\"hljs-string\">\"Nie znam takiej pory roku\"<\/span>);\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p data-sourcepos=\"29:1-29:112\">Most often, however, methods were created to return a result by placing a <code>return<\/code> statement within each <code>case<\/code>.<\/p>\n<div class=\"code-block ng-tns-c4269412455-35 ng-trigger ng-trigger-codeBlockRevealAnimation\">\n<div class=\"code-block-decoration header-formatted gds-title-s ng-tns-c4269412455-35 ng-star-inserted\">\n<p><span class=\"ng-tns-c4269412455-35\">Java<\/span><\/p>\n<div class=\"buttons ng-tns-c4269412455-35 ng-star-inserted\"><\/div>\n<\/div>\n<div class=\"formatted-code-block-internal-container ng-tns-c4269412455-35\">\n<div class=\"animated-opacity ng-tns-c4269412455-35\">\n<pre class=\"ng-tns-c4269412455-35\"><code class=\"code-container formatted ng-tns-c4269412455-35\" role=\"text\" data-test-id=\"code-content\" data-sourcepos=\"31:1-46:1\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> String <span class=\"hljs-title\">getTranslation<\/span><span class=\"hljs-params\">(String season)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">switch<\/span> (season) {\r\n        <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Spring\"<\/span>:\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"wiosna\"<\/span>;\r\n        <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Summer\"<\/span>:\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"lato\"<\/span>;\r\n        <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Autumn\"<\/span>:\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"jesie\u0144\"<\/span>;\r\n        <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-string\">\"Winter\"<\/span>:\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"zima\"<\/span>;\r\n        <span class=\"hljs-keyword\">default<\/span>:\r\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"nieznany\"<\/span>;\r\n    }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p data-sourcepos=\"48:1-48:44\">Ultimately, the call would be a single line:<\/p>\n<div class=\"code-block ng-tns-c4269412455-36 ng-trigger ng-trigger-codeBlockRevealAnimation\">\n<div class=\"code-block-decoration header-formatted gds-title-s ng-tns-c4269412455-36 ng-star-inserted\">\n<p><span class=\"ng-tns-c4269412455-36\">Java<\/span><\/p>\n<div class=\"buttons ng-tns-c4269412455-36 ng-star-inserted\"><\/div>\n<\/div>\n<div class=\"formatted-code-block-internal-container ng-tns-c4269412455-36\">\n<div class=\"animated-opacity ng-tns-c4269412455-36\">\n<pre class=\"ng-tns-c4269412455-36\"><code class=\"code-container formatted ng-tns-c4269412455-36\" role=\"text\" data-test-id=\"code-content\" data-sourcepos=\"50:1-52:46\">String translation = getTranslation(<span class=\"hljs-string\">\"Winter\"<\/span>);<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h2 data-sourcepos=\"5:1-5:74\">FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the purpose of the switch statement in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The switch statement allows you to execute different blocks of code based on the value of an expression, making it easier to handle multiple conditions clearly and efficiently.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What happens if I don\u2019t use break in a switch case?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Without a break, the code continues executing the next case statements until a break or the end of the switch block is reached. This is called a \u201cfall-through.\u201d<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can I use Strings and Enums in a switch statement?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. Starting from Java 7, Strings can be used in switch statements, and Enums have been supported since earlier versions.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn to use the switch Command to control the flow of your program. In Java, the if..else..if ladder executes code from many blocks.\u00a0 The switch Command can replace long ladders that make the code legible. The syntax of the switch Command is as follows: switch (variable \/ expression) { case [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5219,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5218"}],"version-history":[{"count":7,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5218\/revisions"}],"predecessor-version":[{"id":264148,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5218\/revisions\/264148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5219"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}