String in Switch Case in Java

The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated types ( Enums in java), the String class, and Wrapper classes.
Hence the concept of string in switch statement arises into play in JDK 7 as we can use a string literal or constant to control a switch statement, which is not possible in C/C++. Using a string-based switch is an improvement over using the equivalent sequence of if/else statements. We now declare a string as a String class object only as depicted below:
Illustration:
String zambiatek = "zambiatek" ; // Valid from JDK7 and onwards Object zambiatek = "zambiatek" ; // Invalid from JDK7 and onwards
There are certain key points that are needed to be remembered while using switch statement as it does provide convenience but at the same time acts as a double sword, hence we better go through traits as listed:Â
1. Expensive operation: Switching on strings can be more expensive in terms of execution than switching on primitive data types. Therefore, it is best to switch on strings only in cases in which the controlling data is already in string form.
2. String should not be NULL: Ensure that the expression in any switch statement is not null while working with strings to prevent a NullPointerException from being thrown at run-time.
3. Case Sensitive Comparison: The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the equals() method of String class consequently, the comparison of String objects in switch statements is case sensitive.
4. Better than if-else: The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.
Example 1:
Java
// Java Program to Demonstrate use of String to// Control a Switch StatementÂ
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] args)    {Â
        // Custom input string        String str = "two";Â
        // Switch statement over above string        switch (str) {Â
        // Case 1        case "one":Â
            // Print statement corresponding case            System.out.println("one");Â
            // break keyword terminates the            // code execution here itself            break;Â
        // Case 2        case "two":Â
            // Print statement corresponding case            System.out.println("two");            break;Â
        // Case 3        case "three":Â
            // Print statement corresponding case            System.out.println("three");            break;Â
        // Case 4        // Default case        default:Â
            // Print statement corresponding case            System.out.println("no match");        }    }} |
two
Example 2:
Java
// Java Program to Demonstrate use of String to// Control a Switch StatementÂ
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] args)    {Â
        // Custom input string        // Null string is passed        String str = "";Â
        // Switch statement over above string        switch (str) {Â
        // Case 1        case "one":Â
            // Print statement corresponding case            System.out.println("one");Â
            // break keyword terminates the            // code execution here itself            break;Â
        // Case 2        case "two":Â
            // Print statement corresponding case            System.out.println("two");            break;Â
        // Case 3        case "three":Â
            // Print statement corresponding case            System.out.println("three");            break;Â
        // Case 4        // Default case        default:Â
            // Print statement corresponding case            System.out.println("no match");        }    }} |
no match
This article is contributed by Gaurav Miglani. If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



