String in Switch Statement-Java 7 Feature


Developers working on Switch-Case, often wonder why they can’t use Strings in switch statements. With many new features provided with Java 7.0 there is an enhancement in the implementation of switch statement as well. We can now use Strings in the switch statement.

As I mentioned in one of my previous article on Strings in Java, strings are actually integral part of Java. Prior to Java 7 the condition of the switch had to be either a non-long integer type (byte/Byte, short/Short, char/Character and int/Integer), or an enumerated type or a developer had to fall back on if-else statement which gets clumsy once number of cases increase.

Having String in Switch statment is a pretty straight forward feature of Java 7. Let see an example of how to use String inside switch and case statement in JDK7 :

Let’s first make a simple program using if-else:

public class GoyalsBit {

  public static void main(String[] args) {
    int number = 5;
    String value = null;

    switch (number) {
      case 2:
         value = "two";

      case 4:
         value = "four";

      case 5:
         value = "five";

      case 7:
         value = "seven";

         value = "no match found";
      System.out.println("You have entered " + value);

This will print “You have entered five”. Similarly on changing the value of number the output will change.
Developer Check Point- In case we will use long and float here, we will get compile time error.

Now let’s tweak this program a little bit and modify the values for String and integer and see how it works:

public class GoyalsBit{

  public static void main(String[] args) {

    int number = 0;
    String value = “five”;

    switch (value) {

      case “two”:
        number = 2;

      case “four”:
         number = 4;

      case “five”:
         number = 5;

      case “seven”:
         number = 7;

         number = 0;

   System.out.println("Your numeric value is " + number);


The output in this case is “Your numeric value is 5“. We simply changed the int to String.

Things to keep in mind while using switch with Strings:

1) Always perform a null check before the switch statement. In the above example if we do String value=null then you will get an NullPointer Exception
2) Since change in case leads to totally different value in String, there should be a proper check on case of String values. Also, Strings in Switch are case sensitive.
3) We can’t use null in case condition, this will generate compile time error.

The switch statements are alternatives to nested if-else statements. But they are far more optimized as compare to a simple set of if-else statements. Reason being:

1) In if else version jvm internally compares the string by calling the equals() method for each condition, until it is found.
2) In the enhanced switch version (using String) code generates bytecode that uses both the hashCode() and equals() methods. The hash code is used to generate faster switch lookups; i.e. to avoid a chain of equals checks like you would get with an if-else chain OR making it more simple. It uses hashCode() method for switching and equals() method for verification (this is look up switching). So code processing is much faster.
Compilation of switch statements uses the tableswitch and lookupswitch instructions. And this is very well explained in this article :

So overall, it is a small but useful feature which not only helps us write more readable code but the compiler will likely generate more efficient bytecode as compared to the if-then-else statement.