Java Switch String

The switch statement is a beautiful alternative to multiple if else, when all we want to do is match a variable's value to one of many values, to determine what to do. The switch statement is both concise, readable, and have better performance compared to multiple if else version. One issue though, before Java 7 was introduced, we can't use String value with switch statements. That is not fun as String is a well used data type. Sometimes we don't want to define enum and just want to work with Strings. Below are some way on how to use String in Java Switch statements.

Java 6 and Below Java Switch String Version

As mentioned in the introduction, Strings are not allowed in Switch statements in Java version 6 and below. So how do we do a similar effect when our environment is below Java 7? Well, it's the good old switch statement of course! Below is a simple example on how to use if else statement to simulate Java Switch String statements:
String item = "fruit";
if (item.equals("vegetable")) {
    System.out.println("Sorry, we are out of vegetables");
} else if (item.equals("fruit")) {
    System.out.println("What fruit do you want?");
} else {
    System.out.println("We don't have that item.");
}
So depending on the value of the variable item, we want to perform different type of codes. For now, we just have different String to output to the screen. But later in our program, this could be anything. Like if one if is satisfied, we could read contents from a file, if another value then maybe upload something to an FTP server! But to not complicate our example, let's just have different SysOut Strings. As seen above
  • First if is to match the item with vegetable, which if satisfied will tell the customer that we are out of vegetables.
  • The second if statement tries to match item with fruit, which when satisfied will further ask which fruit
  • The last else statement is a catch all if the first two are not satisfied. Which then just simply state it doesn't have the item

Naturally, our code will match the second statement, hence the below expected output:

What fruit do you want?

Java Switch String version 7 and above

The good thing is starting Java 7, switch statement involving String values are allowed. Hence, we could transform the code above to the code below:

String item = "fruit";
switch (item) {
    case "vegetable":
        System.out.println("Sorry, we are out of vegetables");
        break;
    case "fruit":
        System.out.println("What fruit do you want?");
        break;
    default:
        System.out.println("We don't have that item.");
}

The code now is easier to understand because the structure is better and simpler. We expect it to have better performance too. But for those who are still confused, here is what it does?
  • Match vegetable first. If matched, then do the code in it's block and then exit the switch statement.
  • If the first case failed, match fruit next. If matched, then do the code in it's block and then exit the switch statement.
  • If nothing is match, then do the default block.
Not surprisingly,the output should be the same:
What fruit do you want?
Note that the break statement is very important as it exits the switch statement. if we omit this, then the matching will just continue to go. For example, if we run the code below:
String item = "fruit";
switch (item) {
    case "vegetable":
        System.out.println("Sorry, we are out of vegetables");
    case "fruit":
        System.out.println("What fruit do you want?");
    default:
        System.out.println("We don't have that item.");
}
After matching the second value, it execute it's block and then continues to the next, which will result to executing the default section too. Hence we get the below output:
What fruit do you want?
We don't have that item.

And if the first item was matched, like shown below:

String item = "vegetable";
switch (item) {
    case "vegetable":
        System.out.println("Sorry, we are out of vegetables");
    case "fruit":
        System.out.println("What fruit do you want?");
    default:
        System.out.println("We don't have that item.");
}
It will execute all the cases as it matched the first one. Which is not good if it is not what we intended it to do.
Sorry, we are out of vegetables
What fruit do you want?
We don't have that item.