Java String in switch case examples

String in switch case

Java7 version has been introduced using a string in the switch case.

Before the Java7 version, Switch case allows only Integer, byte, short, char variables, and enumeration values only. Support of String is not allowed.

This feature is handy for developers to use strings directly in switch cases and simplify the developers in readability.

Let us see the following code.

public class SwitchWithStringsDemo {
    public static void main(String[] args) {
        int days = 1;
        String s="one";
        String numberValue;
        switch (s) {
            case 1:  numberValue = "ONE";
                break;
            case 2:  numberValue = "TWO";
                break;
            case 3:  numberValue = "THREE";
                break;
            default: numberValue = "Invalid number";
                break;
        }

    }
}

What will happen if we compile the above program in eclipse/net beans using Java 1.7 version? Let us see the output of the above program in the command line?

Eclipse error using String in switch case before JDK 7 version

If we use directly use String in the switch case, the eclipse /net beans code editor throws the following error.

Can not switch on a value of type string for source-level below 1.7. Only convertible int values or enum constants are permitted.

Compile time error with using String in switch case

When the java program using strings in the switch is compiled with the below java7 version, the below error is thrown on the command line.

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – strings in the switch are not supported in -source 1.6 (use -source 7 or higher to enable strings in switch)

Advantages of Using String in Switch case feature in Java 7

An alternative to Switch case is if else statements, but it is not that much readable as well as a confusing developer in terms of more lines of code and cyclomatic complexity is increased.

Cyclomatic complexity is a method to identify the complexity of the code.

We have different alternative methods for using in Strings in switch case

An alternative to strings in Switch case using if-else below Java 1.7

It is not exactly equal to String usage in the Switch case, but an alternative to the switching case.

public class IfElseDemo {
    public static void main(String[] args) {
        String value = "ONE";
        if ("ONE".equals(value)) {
            System.out.println("Message One");
        }
        if ("TWO".equals(value)) {
            System.out.println("Message One");
        }
        if ("THREE".equals(value)) {
            System.out.println("Message One");
        }
    }
}

An alternative to String in Switch case example using enums

Before Java 7, enum’s can be mapped to switch cases and the below code specifies the usage of an enum.

public class AlternativeToStringSwitch {

    public enum Daily {
        ONE, TWO, THREE;
    }
    public static void main(String args[]) {
        String str = "ONE";
        switch (Daily.valueOf(str)) {
            case ONE:
                System.out.println("one");
                break;
            case TWO:
                System.out.println("two");
                break;
            case THREE:
                System.out.println("three");
                break;
        }
    }
}

Example of String in switch case in java7

Here is an example for switch case string tutorial in java7

public class SwitchStringDemoDemo {
    public static void main(String[] args) {
        int days = 1;
        String string="one";
        String numberValue;
        switch (string) {
            case "one":  numberValue = "ONE";
                break;
            case "two":  numberValue = "TWO";
                break;
            case "three":  numberValue = "THREE";
                break;
            default: numberValue = "Invalid number";
                break;
        }

    }
}

In Java 7, variables of Strings are assigned and can be referenced in the Switch case. Case Strings values are constants as before in the jdk6 version.

Handle Null check for Strings in Switch case in java 7

As with other Java code, if null is passed as String in the Switch case, NullPointerException is thrown. We have to handle the null checks for strings passed.

Conclusion

This tutorial talks about using a string in switch case in java7 and also includes alternatives to this prior java7 versions with examples.