Switch Statement in Java

As if-else statement tells your program to execute a certain section of code only if a particular test evaluates to true or false, the Java switch statement can have multiple possible execution paths.

A switch works with the –

  • primitive data types e.g. byte, short, char, and int
  • wrapper classes e.g. Character, Byte, Short, and Integer
  • enumerated types (added in java 5)
  • String class (added in java 7)

1. Java switch statement syntax

The general form of a switch statement is –

switch (switch-expression) 
{
        case label-1:
                statements;
 		break;

        case label-2:
                statements;
 		break;

        case label-3:
                statements;
 		break;

        default:
                statements;
}

Here label-1, label-2, etc. are “compile-time constant expressions” (the value of the labels must be known at compile time).

We can achieve the similar functionality with nested if-else blocks, but switch statement is more readable and clean.

2. Java switch statement execution flow

A switch statement is evaluated as follows:

  1. The switch-expression is evaluated.
  2. If the value of the switch-expression matches a case label, the execution starts from the matched case label and executes all statements until the break statement is encountered.
  3. If the value of the switch-expression does not match a case label, execution starts at the statement following the optional default label and continues until the end of the switch statement or the break statement is encountered.

You can easily make out that the use of a break statement inside the default label is not necessary because the default label is the last label in the switch statement and the execution of the switch statement will stop after that anyway.

3. Expression values must be in range

Please note that the value of the constant expressions used as the case labels must be in the range of the data type of switch-expression.

The range of the byte data type in Java is -128 to 127, so the following code would not compile because the second case label is 150, which is outside the range of the byte data type:

byte b = 10;
switch (b) {
        case 5:
                b++;
                break;

        case 150:       // A compile-time error. 150 is greater than 127
                b--;
                break;

        default:
                b = 0;
}

4. Distinct labels

Another important point to note is that two case labels in a switch statement cannot be the same. The following piece of code would not compile because case label 10 is repeated:

int num = 10;
switch (num) {
        case 10:
                num++;
                break;

        case 10: // A compile-time error. Duplicate case label 10
                num--;
                break;

        default:
                num = 100;
}

That’s all for switch statement in java.

Happy Learning !!

Read More : Java Control Statements

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.