Java Switch Statement

Video Tutorial
FREE
Switch case thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Switch statement in Java is used to select one of the many code blocks to be executed. The value of the expression in switch case in Java is compared with the values of each case.

If there is a match, the associated block of code is executed. Switch case in Java is fall-through which means it executes all the cases after the first match if a break statement is not encountered.

Switch Statement in Java

Some Important Points to Remember

Let’s quickly go through some standard rules for writing switch cases in Java.

  • No variables: The case value in switch statement must be a literal or constant. Also, it must be of the same type as switch expression.
  • No duplicates: No two cases in a switch statement should be of same value. Otherwise, compilation error is thrown.
  • Allowed Types: The Java switch expression must be of int, long, byte, short, enums, and String type. Primitives are allowed with their wrapper types.
  • Optional Break Statement: Break statement is optional in Java's switch statement. If a case is matched and there is no break statement mentioned, subsequent cases are executed until a break statement or end of the switch statement is encountered.
  • Optional default case: default case value is optional. The default statement is meant to execute when there is no match between the values of the variable and the cases. It can be placed anywhere in the switch block according to the required result.

Syntax

Let us understand the usage of switch case in Java programmatically.

Example

Consider the code snippet given below:

Output:

Explanation:

  • The value of the day variable is compared with each of the case values. Since day = 4, it matches the fourth case value, and Day 4: Thursday is printed.
  • The break statement in the fourth case breaks out of the switch statement.

The break Keyword

When the Java program reads the break statement it comes out of the switch block immediately with no further comparisons. The execution of any more code within the switch-case block is seized and case testing/comparison is stopped.

The default Keyword

In switch case in Java, the default statement is executed when there is no single comparison that checks out which means the value of variable and the case value don’t match in of any cases. In this scenario, suppose the day variable is given the value 9 then all the cases will be checked and since equality conditions cannot be achieved with this value, the default statement will get executed at the end.

Example:

Output:

Java Nested Switch Statement

A nested switch case in a java statement refers to a scenario where a switch statement is used inside another switch statement. Basically, it is switch statement within a switch statement.

Example:

Output:

Conclusion

  • The Switch case in Java is the comparison between a variable and its possible values.
  • Break statement is used to come out of the switch block immediately without any further comparisons.
  • Default statement is executed only when there is no case with value same as the variable value.
  • Switch case is called a fall-through statement when we miss out break statements in switch cases.
  • We can nest one switch statement within another switch statement with independent variable values.
  • We can use enum and wrapper classes in switch statements in Java.