Switch statement in Java

Switch statement is a multi-way branch statement i.e. it executes one out of many possible branches of the code. It works similar to if-else-if ladder.


Control Flow Diagram

The control flow diagram for switch statement is shown below:

 

Control Flow in switch statement.
switch statement

 


Syntax

General form of the switch statement is:

Note following things before moving forward:

  • Each of the case values must be unique. if not, Java will throw compile time error.
  • Each of the case values must be type compatible with the type of expression. The type values can be literal or constant. It cannot be a variable.
  • Each of the case statements can have an optional break statement. If present, it terminates the switch block when control reaches there, else next case is executed and so on.
  • Default label is optional. It runs only when none of the cases match the expression value. If absent and none of the cases match then nothing happens

An Example

The output is:

Let’s discuss the way it works. If the switch encounters any of 12, 1 or 2, it will run the print statement on line 9 then it will encounter a break and the control flows out of switch.  If it encounters any of 3, 4 or 5, it will run the print statement on line 14 then it will encounter a break and the control flows out of switch. And so on. If it doesn’t match any of 1 to 12, then the statements inside default are run, encounters a break and the control flows out of switch.

  • You can put default clause at any place inside switch.
  • To avoid unpleasant surprises, always write default as the last statement. (In such a situation, even if you miss the break inside default clause, it wont hurt because it was the last statement and switch would break anyway.)

If you write default before any other case, always include a break statement. If you don’t, switch will run all statements unless it finds a break or it encounters the end. Like in the example below:

The output is:

The way it works is: switch finds 25 as the month value, it doesn’t match any of the cases, so it runs the default statement but doesn’t find a break there, so it keeps running other statements (on line 17) unless it finds a break.


Variations in switch Expression

  • Switch expression can only be of the following type:
    • primitive data types like: byte, short, int, long, char etc.
    • String (starting Java 7)
    • Enumerations
    • Type Wrappers like: Byte, Short, Integer, Long, Character etc.
Switching on String

Starting JDK-7, Java allows us to switch on Strings. Note that all the case statements should be string literals in this case.

The output is:

Switching on Enumerations

Java also allows switching on enumerations.

The output is:

There are a few things to note here:

All the case statements in a switch statement should use constants from the same enum as the one used by the switch expression.

The names of the enumeration constants are used without being qualified by the enumeration type. It happns so because their type is implicitly defined to that used by the expression inside switch. The names should not be qualified by the enums and if you do so, compiler will throw errors.

Switching on Type Wrappers

Java allows us to switch on Type Wrapper Classess like: Byte, Short, Integer, Long and Character.

The output is:

 


Nested Switch Statements

Switch statements can be nested similar to  nested-if statements.

Simply put, if a switch statement is contained inside another switch statement then the switch statements are said to be nested.

The control flow diagram for nested switch statements is shown below:

 

Control flow in nested switch statement
Nested switch statement – Diagram shows two level nesting.

 

In the control-flow diagram above, one of the cases contains a switch statement instead of normal statements.

The output is:

The above code congratulates only those students who get an ‘A’. Since the grade value for Tom was ‘A’, control enters the switch case corresponding to ‘A’ (first switch-case). But another switch is encountered there and based on the name, greetings are printed inside the internal switch statement.

 


Differences between if-else-if ladder and switch

  • Switch is more readable than if-else-if ladder.
  • Switch statements are often more efficient than a set of if-statements due to compiler optimizations.
  • But it tests only  for equality and not order(like > or < ). If-else can evaluate any form of boolean expressions.
  • Unlike if-else-if ladder which runs only one of the all possible cases, it will keep on testing other cases and running other statements, if it doesn’t get a break.
  • In a switch statement, no two cases can have the same identical switch constants. However, if switch statements are nested then an inner switch can have same switch values as the outer switch cases.