Title: Java Switch Case Example - Java Code Geeks
Open Graph Title: Java Switch Case Example - Java Code Geeks
Description: Check out our detailed example on Java Switch and how to use the switch case statement to control the flow of your program!
Open Graph Description: Check out our detailed example on Java Switch and how to use the switch case statement to control the flow of your program!
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-switch-case-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-switch-case-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/"},"author":{"name":"Konstantina Dimtsa","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56"},"headline":"Java Switch Case Example","datePublished":"2014-01-10T11:46:53+00:00","dateModified":"2023-11-09T12:37:48+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/"},"wordCount":441,"commentCount":2,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["switch statement"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-switch-case-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-switch-case-example/","url":"https://examples.javacodegeeks.com/java-switch-case-example/","name":"Java Switch Case Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-01-10T11:46:53+00:00","dateModified":"2023-11-09T12:37:48+00:00","description":"Check out our detailed example on Java Switch and how to use the switch case statement to control the flow of your program!","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-switch-case-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-switch-case-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-switch-case-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-switch-case-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Java Basics","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/"},{"@type":"ListItem","position":4,"name":"switch statement","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/switch-statement/"},{"@type":"ListItem","position":5,"name":"Java Switch Case Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56","name":"Konstantina Dimtsa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","caption":"Konstantina Dimtsa"},"description":"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.","sameAs":["http://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/konstantina-dimtsa/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Java Basics","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/"}},{"@type":"ListItem","position":4,"item":{"name":"switch statement","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/switch-statement\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-01-10T13:46:53+02:00","datePublished":"2014-01-10T13:46:53+02:00","dateModified":"2023-11-09T14:37:48+02:00","headline":"Java Switch Case Example","name":"Java Switch Case Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-switch-case-example\/","description":"In this post, we feature a comprehensive Java Switch Case Example. Java provides decision making statements so as to control the flow of your program. The switch statement in java is the one that we w","copyrightYear":"2014","articleSection":"switch statement","articleBody":"\nIn this post, we feature a comprehensive Java Switch Case Example. Java provides decision making statements so as to control the flow of your program. The switch statement in java is the one that we will explain in this article. These statements are:\n\n\n\n\nif...then\n\n\n\nif...then...else\n\n\n\nswitch..case\n\n\n\n\n\n\n\n\nA switch statement in java checks if a variable is equal to a list of values. The variable in the switch statement can be a byte, short, int, or char. However, Java 7 supports also switch statements with Strings. We will see such an example in the next sections.\n\n\n\n1. Syntax of Java switch case\n\n\n\nThe syntax of a switch case statement is the following:\n\n\n\nswitch (variable) {\n case c1:\n statements \/\/ they are executed if variable == c1\n break;\n case c2: \n statements \/\/ they are executed if variable == c2\n break;\n case c3:\n case c4: \n statements \/\/ they are executed if variable == any of the above c's\n break;\n . . .\n default:\n statements \/\/ they are executed if none of the above case is satisfied\n break;\n}\n\n\n\n\nswitch: the switch keyword is followed by a parenthesized expression, which is tested for equality with the following cases. There is no bound to the number of cases in a switch statement.\n\n\n\ncase: the case keyword is followed by the value to be compared to and a colon. Its value is of the same data type as the variable in the switch. The case which is equal to the value of the expression is executed.\n\n\n\ndefault: If no case value matches the switch expression value, execution continues at the default clause. This is the equivalent of the \"else\" for the switch statement. It is conventionally written after the last case, and typically isn't followed by break because execution just continues out of the switch. However, it would be better to use a break keyword to default case, too. If no case matched and there is no default clause, execution continues after the end of the switch statement.\n\n\n\nbreak: The break statement causes execution to exit the switch statement. If there is no break, execution flows through into the next case, but generally, this way is not preferred.\n\n\n\n\n2. Example of switch case\n\n\n\nLet's see an example of the switch case. Create a java class named SwitchCaseExample.java with the following code:\n\n\n\nSwitchCaseExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n\n\n\npackage com.javacodegeeks.javabasics.switchcase;\n \npublic class SwitchCaseExample {\n public static void main(String[] args) {\n \n grading('A');\n grading('C');\n grading('E');\n grading('G');\n }\n \n public static void grading(char grade) {\n \n int success;\n switch (grade) {\n case 'A':\n System.out.println(\"Excellent grade\");\n success = 1;\n break;\n case 'B':\n System.out.println(\"Very good grade\");\n success = 1;\n break;\n case 'C':\n System.out.println(\"Good grade\");\n success = 1;\n break;\n case 'D':\n case 'E':\n case 'F':\n System.out.println(\"Low grade\");\n success = 0;\n break;\n default:\n System.out.println(\"Invalid grade\");\n success = -1;\n break;\n }\n \n passTheCourse(success);\n \n }\n \n public static void passTheCourse(int success) {\n switch (success) {\n case -1:\n System.out.println(\"No result\");\n break;\n case 0:\n System.out.println(\"Final result: Fail\");\n break;\n case 1:\n System.out.println(\"Final result: Success\");\n break;\n default:\n System.out.println(\"Unknown result\");\n break;\n }\n \n }\n \n}\n\n\n\n\n\n\n\n\n\n\nIn the above code we can see two switch case statements, one using char as data type of the expression of the switch keyword and one using int.\n\n\n\nOutput\n\n\n\nExcellent grade\nFinal result: Success\nGood grade\nFinal result: Success\nLow grade\nFinal result: Fail\nInvalid grade\nNo result\n\n\n\n\nBelow is the equivalent of the switch case statement in the method passTheCourse() using if..then..else:\n\n\n\n\n\n\n\n\n\n1\n2\n3\n4\n5\n6\n7\n8\n9\n\n\n\nif (success == -1) {\n System.out.println(\"No result\");\n} else if (success == 0) {\n System.out.println(\"Final result: Fail\");\n} else if (success == 1) {\n System.out.println(\"Final result: Success\");\n} else {\n System.out.println(\"Unknown result\");\n}\n\n\n\n\n\n\n\n\n\n\n3. Example of switch case using String\n\n\n\nAs we mentioned in the introduction of this example, Java SE 7 supports String in switch case statements. Let's see such an example. Create a java class named StringSwitchCase.java with the following code:\n\n\n\nStringSwitchCase.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n\n\n\npackage com.javacodegeeks.javabasics.switchcase;\n \npublic class StringSwitchCase {\n \n public static void main(String args[]) {\n \n visitIsland(\"Santorini\");\n visitIsland(\"Crete\");\n visitIsland(\"Paros\");\n \n }\n \n public static void visitIsland(String island) {\n switch(island) {\n case \"Corfu\":\n System.out.println(\"User wants to visit Corfu\");\n break;\n case \"Crete\":\n System.out.println(\"User wants to visit Crete\");\n break;\n case \"Santorini\":\n System.out.println(\"User wants to visit Santorini\");\n break;\n case \"Mykonos\":\n System.out.println(\"User wants to visit Mykonos\");\n break;\n default:\n System.out.println(\"Unknown Island\");\n break;\n }\n }\n \n}\n\n\n\n\n\n\n\n\n\n\nIf we run the above code, we will have the following result:\n\n\n\nOutput\n\n\n\nUser wants to visit Santorini\nUser wants to visit Crete\nUnknown Island\n\n\n\n\n4. More articles\n\n\n\n\nSimple while loop Java Example\n\n\n\nFor Each Loop Java 8 Example\n\n\n\nFor loop Java Example\n\n\n\n\n5. Download the source code\n\n\n\nThis was a Java Switch-Case Example. \n\n\n\nDownload\nYou can download the source code from here: Java Switch Case Example\n\n\n\nLast updated on Apr. 29th, 2021\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-switch-case-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Konstantina Dimtsa","url":"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-01-10T11:46:53+00:00 |
| article:modified_time | 2023-11-09T12:37:48+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Konstantina Dimtsa |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Konstantina Dimtsa |
| twitter:label2 | Est. reading time |
| twitter:data2 | 3 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1