Switch Case in Java

  • Last updated Apr 25, 2024

The switch statement is a control flow statement that is used when there are multiple options to be tested for equality against a single switch variable and each option may have a different task to perform. It provides an alternative to using multiple if-else statements when there is a limited number of possible values to check against.

 A switch statement works with the byte, short, char, int primitive data types, enum Types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Here's the general syntax of a switch statement in Java:

switch (expression) {
   case value1:
       // code block to execute when the expression matches value1
   case value2:
       // code block to execute when the expression matches value2
   case value3:
       // code block to execute when the expression matches value3
   case value4:
       // code block to execute when the expression matches value4
       // code block to execute when none of the cases match the expression

Here's an explanation of the above switch syntax:

  • switch: It is the keyword for using a switch statement. It is used to select one of many code blocks to be executed based on the value of an expression.
  • expression: It is evaluated and compared to the values specified in the "case" labels.
  • case: It represents a specific value that is compared to the expression. If a match is found, the code block following that "case" label will be executed.
  • break: It is used to exit the "switch" statement and prevent the execution of subsequent code blocks.
  • default: The default keyword in switch statement specifies a block of code to run if there is no case match in the switch. The use of the default keyword is optional.

When the switch statement is executed, the expression is evaluated, and its value is compared against the case labels. If a case label matches the expression's value, the corresponding code block is executed until a break statement is encountered. The break statement is essential as it terminates the switch statement and continues execution after the switch block. If none of the case labels match the expression, the code block following the default label is executed. Including a default case is optional.

Switch statements is a good choice to handle multiple possible values of an expression, avoiding lengthy if-else chains.


Here's an example to demonstrate how a switch statement works in Java:

public class SwitchExample {

    public static void main(String[] args) {
        int month = 5;
        String monthString;

        switch(month) {
            case 1:
                monthString = "January";

            case 2:
                monthString = "February";

            case 3:
                monthString = "March";

            case 4:
                monthString = "April";

            case 5:
                monthString = "May";

            case 6:
                monthString = "June";

            case 7:
                monthString = "July";

            case 8:
                monthString = "August";

            case 9:
                monthString = "September";

            case 10:
                monthString = "October";

            case 11:
                monthString = "November";

            case 12:
                monthString = "December";

                monthString = "Invalid month";





In this example, an integer variable month is declared and assigned a value of 5, representing the month of May. A string variable monthString is declared to store the corresponding month name. The switch statement starts with the keyword switch followed by the expression month in parentheses. It will determine the flow based on the value of the month variable. Each case label represents a possible value that the expression month can have. For example, case 1 represents the value 1, which corresponds to January. Within each case block, the corresponding month name is assigned to the monthString variable. For example, in case 5, monthString is assigned the value "May". The break statement is used after each case to exit the switch statement. It prevents the execution from "falling through" to the next case block. After the switch block, there's a default case. If the value of month doesn't match any of the cases, the code block under default is executed, and monthString is set to "Invalid month". Finally, the value of monthString is printed to the console using System.out.println(monthString).

In this specific example, since the value of month is 5, the corresponding case case 5 is matched, and monthString is set to "May". Therefore, when the program is run, it will output "May" to the console.

Nesting of switch statement is also possible, which means creating of the switch statement inside another switch statement.