TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Case Keyword

Use the case keyword to select values in a switch. Stack cases and fall through to the next case.
Case. This keyword is part of a switch. A case's inner statements are executed when the value switched upon has the value of the case. We can use final values and constants in cases.KeywordsSwitch
Strings, ints, and chars are some of the variable types we can match in a case. A case usually has a break or return at its end. But if it does not, it falls through to the next case.String Switch
A complex example. This example uses many case statements. It stacks cases, which means multiple values (100, 1000, or 10000 for example) all match the same statement block.

Braces: The curly brackets are optional around the statements of a case. We can even have multiple statements, and no braces are required.

Finally: A final constant value (like the special string in this example) can be matched in a case.

Expression: An expression that can be compiled into a constant (like "cat" plus "100") is also allowed as the case value.

Java program that uses case statements public class Program { static String testCase(String value) { final String special = "constant"; String temp = ""; // Switch on the String value. switch (value) { case "100": case "1000": case "10000": { // Place braces around case statements. return "Multiple of ten"; } case "500": case "5000": case "50000": // No braces are needed. return "Multiple of fifty"; case special: // We can match a final constant. // ... Multiple statements can be in a case. String result = special.toUpperCase() + "!"; return result; case "cat" + "100": // This also matches a constant. // ... The string expression is compiled into a final String. return "CAT"; case "X": // This case will fall through so case "Y" is also entered. temp += "X"; case "Y": temp += "Y"; return temp; default: // The default case. return "Invalid"; } } public static void main(String[] args) { System.out.println(testCase("100")); System.out.println(testCase("1000")); System.out.println(testCase("5000")); System.out.println(testCase("constant")); System.out.println(testCase("cat100")); System.out.println(testCase("X")); System.out.println(testCase("Y")); System.out.println(testCase("?")); } } Output Multiple of ten Multiple of ten Multiple of fifty CONSTANT! CAT XY Y Invalid
Locals. In the testCase() method, we have a local variable String called temp. We build this up in the "X" and "Y" cases. When "X" is matched, the result is "XY."

Note: The case "X" has no termination statement (no break or return) so it falls through and also matches case "Y."

Return
Symmetry. In switch statement and cases, we have an opportunity to increase our program quality by adding symmetry. Each case can be seen as a separate but equal path.

And: In Java we can have all cases use the same bracket syntax so all statements are visually equal.

Tip: This can improve program readability. And readability in turn reduces the changes a program will have nasty bugs.

In Eclipse, cases can be hard to format correctly without help. Try selecting the Source menu. Then click Format—the cases will be indented.
A review. The case keyword in Java can handle constants (finals) and constant expressions. Default too is a case, but it omits the "case" keyword.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf