Java Switch Statement

1. Overview

In this tutorial, we’ll learn what the switch statement is and how to use it.

The switch statement allows us to replace several nested if-else constructs and thus improve the readability of our code.

Switch has evolved over time – new supported types have been added, particularly in Java 5 and 7. Also, it continues to evolve – switch expressions will likely be introduced in Java 12.

Below we’ll give some code examples to demonstrate the use of the switch statement, the role of the break statement, the requirements for the switch argument/case values and the comparison of Strings in a switch statement.

Let’s move on to the example.

2. Example of Use

Let’s say, we have the following nested if-else statements:

public String exampleOfIF(String animal) {
    String result;
    if (animal.equals("DOG") || animal.equals("CAT")) {
        result = "domestic animal";
    } else if (animal.equals("TIGER")) {
        result = "wild animal";
    } else {
        result = "unknown animal";
    }
    return result;
}

The above code doesn’t look good and would be hard to maintain and reason about. To improve readability we could make use of a switch statement here:

public String exampleOfSwitch(String animal) {
    String result;
    switch (animal) {
        case "DOG":
            result = "domestic animal";
            break;
        case "CAT":
            result = "domestic animal";
            break;
        case "TIGER":
            result = "wild animal";
            break;
        default:
            result = "unknown animal";
            break;
    }
    return result;
}

As shown above, we compare the switch argument animal with the several case values. If none of the case values is equal to the argument, then the block under the default label is executed.

Simply put, the break statement is used to exit a switch statement.

3. The break Statement

Although most of the switch statements in real life imply that only one of the case blocks should be executed, the break statement is necessary to exit a switch after the block completes.

If we forget to write a break, the blocks underneath will be executed.

To demonstrate this let’s omit the break statements and add the output to the console for each block:

public String forgetBreakInSwitch(String animal) {
    switch (animal) {
    case "DOG":
        System.out.println("domestic animal");
    default:
        System.out.println("unknown animal");
    }
}

Let’s execute this code  forgetBreakInSwitch(“DOG”), and check the output to prove that all the blocks get executed:

domestic animal
unknown animal

So we should be careful and add break statements at the end of each block unless there is a need to pass through to the code under the next label.

The only block where a break is not necessary is the last one, but adding a break to the last block makes the code less error-prone.

We can also take advantage of this behavior to omit break when we want the same code executed for several case statements. Let’s rewrite the example in the previous section by grouping together the first 2 cases:

public String exampleOfSwitch(String animal) {
    String result;
    switch (animal) {
        case "DOG":
        case "CAT":
            result = "domestic animal";
            break;
        case "TIGER":
            result = "wild animal";
            break;
        default:
            result = "unknown animal";
            break;
    }
    return result;
}

4. switch Argument and case Values

Now let’s discuss the allowed types of switch argument and case values, the requirements for them and how the switch statement works with Strings.

4.1. Data Types

We can’t compare all the types of objects and primitives in the switch statement. A switch works only with four primitives and their wrappers, as well as with the enum type and the String class:

  • byte and Byte

  • short and Short

  • int and Integer

  • char and Character

  • enum

  • String

String type is available in the switch statement starting with Java 7.

enum type was introduced in Java 5 and has been available in the switch statement since then.

Wrapper classes have also been available since Java 5.

Of course, switch argument and case values should be of the same type.

4.2. No null Values

We can’t pass the null value as an argument to a switch statement. If we do it, the program will throw NullPointerException, using our first switch example:

@Test(expected=NullPointerException.class)
public void whenSwitchAgumentIsNull_thenNullPointerException() {
    String animal = null;
    Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
}

Of course, we can’t also pass null as a value to the case label of a switch statement. If we do it, the code will not compile.

4.3. Case Values as Compile-Time Constants

If we try to replace the DOG  case value with the variable dog the code won’t compile until we mark the dog variable as final:

final String dog="DOG";
String cat="CAT";

switch (animal) {
case dog: //compiles
    result = "domestic animal";
case cat: //does not compile
    result = "feline"
}

4.4. String Comparison

If a switch statement used the equality operator to compare strings we couldn’t compare a String argument created with the new operator to a String case value correctly.

Luckily, the switch operator uses the equals() method under the hood.

Let’s demonstrate this:

@Test
public void whenCompareStrings_thenByEqual() {
    String animal = new String("DOG");
    assertEquals("domestic animal", s.exampleOfSwitch(animal));
}

5. New switch Expression

Java 12 is available and with it the preview of a new feature – a less verbose switch expression.

In order to enable it, we need to pass –enable-preview to the compiler.

Let’s take a look:

var month = JUN;
var value = switch(month) {
  case JAN, JUN, JUL -> 3;
  case FEB, SEP, OCT, NOV, DEC -> 1;
  case MAR, MAY, APR, AUG -> 2;
};

System.out.println(value); // 3

In order to leverage the new syntax, we no longer use a colon, but the operator known from Lambda Expressions. Notice the lack of break keyword and that fall-through cases is not happening.

But there’s still a possibility to obtain fine-grained control over what’s happening on the right side of the expression by using code blocks. In such a case, we need to manually break from it and specify a return value:

public static void switchLocalVariable() {
    var month = Month.AUG;
    int i = switch (month){
        case JAN,JUN, JUL -> 3;
        case FEB,SEP, OCT, NOV, DEC -> 1;
        case MAR,MAY, APR, AUG -> {
            int j = month.toString().length() * 4;
            break j;
        }
    };
    System.out.println(i); //12
}

6. Conclusion

In this tutorial, we’ve learned the subtleties of using the switch statement in Java. We can decide whether to use switch based on readability and the type of the compared values.

The switch statement is a good candidate for cases when we have a limited number of options in a pre-defined set (eg: days of the week). Otherwise, we’d have to modify the code each time a new value is added or removed, which may not be feasible. For these cases, we should consider other approaches such as polymorphism or other design patterns like Command.

As always the complete code is available over on GitHub.

Leave a Reply

Your email address will not be published.