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 use the switch statement instead:
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; }
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.
A 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.
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.
As always the complete code is available over on GitHub.