Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> 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. switch Expressions

JDK 13 is now available and brings an improved version of a new feature first introduced in JDK 12: the switch expression.

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

5.1. The New switch Expression

Let's see what the new switch expression looks like when switching over months:

var result = switch(month) {
    case JANUARY, JUNE, JULY -> 3;
    case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
    case MARCH, MAY, APRIL, AUGUST -> 2;
    default -> 0; 
};

Sending in a value like Month.JUNE would set result to 3.

Notice that the new syntax uses the -> operator instead of the colon we're used to with switch statements. Also, there's no break keyword: The switch expression doesn't fall through cases.

Another addition is the fact that we can now have comma-delimited criteria.

5.2. The yield Keyword

Going a bit further, there's 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 use the keyword yield:

var result = switch (month) {
    case JANUARY, JUNE, JULY -> 3;
    case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
    case MARCH, MAY, APRIL, AUGUST -> {
        int monthLength = month.toString().length();
        yield monthLength * 4;
    }
    default -> 0;
};

While our example is a bit arbitrary, the point here is that we've got access to more of the Java language here.

5.3. Returning Inside switch Expressions

As a consequence of the distinction between switch statements and switch expressions, it is possible to return from inside a switch statement, but we're not allowed to do so from within a switch expression.

The following example is perfectly valid and will compile:

switch (month) {
    case JANUARY, JUNE, JULY -> { return 3; }
    default -> { return 0; }
}

However, the following code will not compile, as we are trying to return outside of an enclosing switch expression:

var result = switch (month) {
    case JANUARY, JUNE, JULY -> { return 3; }
    default -> { return 0; }
};

5.4. Exhaustiveness

When using switch statements, it doesn't really matter if all cases are covered.

The following code, for example, is perfectly valid and will compile:

switch (month) { 
    case JANUARY, JUNE, JULY -> 3; 
    case FEBRUARY, SEPTEMBER -> 1;
}

For switch expressions though, the compiler insists that all possible cases are covered. The following code snippet, for example, would not compile, as there's no default case and not all possible cases are covered:

var result = switch (month) {
    case JANUARY, JUNE, JULY -> 3;
    case FEBRUARY, SEPTEMBER -> 1;
}

The switch expression, however, will be valid when all possible cases are covered, like in the following example:

var result = switch (month) {
    case JANUARY, JUNE, JULY -> 3;
    case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
    case MARCH, MAY, APRIL, AUGUST -> 2;
}

Please note that the above code snippet does not have a default case. As long as all cases are covered, the switch expression will be valid.

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 JDK 8 code and JDK 13 code is available on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
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
Guest

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
Guest

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.

Comments are closed on this article!