I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

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 Statement

Scheduled for Java 12, we’ll have no need to write break statements and to return the result value, as we do now in the exampleOfSwitch() method, thanks to the new case labels and the switch expressions.

With the new “case L ->” label, only the block to the right of an arrow label will be executed, the blocks underneath won’t.

With the new switch expression, we can assign the whole switch to the result variable and put the values straight to the right of an arrow label. This is a handy feature that other languages like Kotlin/Scala already have.

Also, the scope of a local variable in a case block will be this case block, not the entire switch block.

Keep in mind that this might change until the final release.

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.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

4
Leave a Reply

avatar
2 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Loredana CrusoveanuAleksejaymeric Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
aymeric
Guest
aymeric

hello, I am ok with the fact that nested if/else are evil but Switch may also be a POO problem as it (most of time) violate the Open/Close principle. Having a switch on non-extensible element (like the days of a week) its ok because you will not have to modified your code in case of “extension”. But for other cases, each time you add a new value (in enum for exemple) you have to modified your code instead of enrich it (by implementing a new interface for exemple). This tutorial is usefull for people who wants to learn how to… Read more »

Loredana Crusoveanu
Editor

Hello,

Thanks for the feedback. This was indeed intended as a beginner guide to the switch statement. I’ve added a note with your observation in the Conclusion section.

Cheers.

Aleksej
Guest

Hi to everyone! I noticed a little thing into the the 2. title of this tutorial, which I think, can improve readability even more… In the “2. Example of Use” code example we can group together the 1. and 2. case blocks to avoid the redundancy of the right part of the assignment to the result String variable. Because it’s clear that both DOG and CAT are domestic animals. 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”;… Read more »

Loredana Crusoveanu
Editor

Hey Aleksej,

That’s a great point. I added your example in section 3 as that’s where the behavior of break is explained.

Cheers.