Java Switch Statement

Last Updated : 29 Dec 2025

The Java switch statement executes one statement from multiple conditions. It is like an if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String, and some wrapper types, such as Byte, Short, Integer, and Long. Since Java 7, we can use strings in the switch statement.

What Is switch Statement in Java?

The switch statement can be described as a control flow type statement used to manipulate the flow of program execution and invoke various branches of code based on the value of an expression.

In other words, the switch statement tests the equality of a variable against multiple values.

Rules and Constraints of Java Switch Statement

There are several rules and constraints of Java switch statement. Some of them are as follows:

  • There can be one or N case values for a switch expression.
  • The case value must be of switch expression type only. The case value must be literal or constant. It doesn't allow variables.
  • The case values must be unique. In case of a duplicate value, it renders a compile-time error.
  • The Java switch expression must be of byte, short, int, long (with its Wrapper type), enumsand string.
  • Each case statement can have a break statement, which is optional. When control reaches thebreak statement, it jumps to the control after the switch expression. If a break statement is not found, the next case is executed.
  • The case value can have a default label, which is optional.

In Java, the switch statement provides a more detailed alternative that avoids the use of nested or multiple if-else statements when associated with a single variable.

The syntax of the Java switch statement contains the switch keyword, which is followed by the expression that needs to be evaluated using parentheses. The mentioned expression must definitely evaluate to a definite data type, which is primitive such as int, char, or enum.

Syntax of Switch Statement

Here is the syntax of switch statement:

Flowchart of Switch Statement

The following images demonstrates the working flow of a switch statement:

flow of switch statement in java

In Java, the switch statement can also contain a default label. The default label will be executed only in the situation when none of the case labels match the expression's value. Declaring a default label is considered optional, but it can be useful in the event of unexpected values or inputs.

Now, we are going to discuss several examples to demonstrate the working of Switch Statement in different aspects.

Different Examples of switch Statement

Practice the following examples to understand the concept of switch statement in Java:

Example 1: Simple Switch Statement

Let us take an example to demonstrate the working of the switch statement in Java.

Example

Compile and Run

Output:

20

Example 2: Finding Month using switch Statement

Let us take an example to demonstrate how to find months using the switch statement in Java.

Example

Compile and Run

Output:

7 - July

Example 3: Checking Vowel or Consonant

If the character is A, E, I, O, or U, it is a vowel; otherwise, consonant. It is not case-sensitive.

Example

Compile and Run

Output:

Vowel

Java Switch Statement is a fall-through

The Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present.

Example

Compile and Run

Output:

20
30
Not in 10, 20 or 30

Java Switch Statement with String

Since Java 7, Java allows us to use strings in switch expressions. The case statement should be a string literal.

Example

Compile and Run

Output:

Your Level is: 3

Java Nested Switch Statement

We can use a switch statement inside another switch statement in Java. It is known as a nested switch statement.

Example

Compile and Run

Output:

Data Communication and Networks, MultiMedia

Using enum in Switch Statement

Java allows us to use an enum in a switch statement. A Java enum is a class that represents a group of constants. (immutable, such as final variables). We use the keyword enum and place the constants in curly braces, separated by commas.

Example

Compile and Run

Output:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Java Wrapper in Switch Statement

Java allows us to use four wrapper classes -Byte, Short, Integer, and Long - in the switch statement.

Example

Compile and Run

Output:

You are eligible to vote.

Features and Limitations of Java Switch Statement

There are several features and limitations of the Java Switch Statament. Some of them are as follows:

  • One of the major important features of the Java Switch statement is its fall-through behaviour. It means that if a case label does not contain a break statement, execution will be passed directly to the next case label. A switch statement can also include a default label, which is executed if none of the case labels match the expression's value. The default label is optional but can be useful for handling unexpected or unspecified values.
  • Switch statements can only match exact values, and they cannot check ranges of values. This means that if you need to check for a range of values, you would need to use multiple case labels or resort to other control flow statements, such as if-else.
  • Switch statements can only be used to check for equality between the expression and the case labels. They cannot perform more complex checks, such as checking conditions or using comparison operators.
  • The expression in a switch statement must evaluate to a primitive data type (int, char, or enum) or a String (since Java 7). This limitation restricts the types of expressions that can be used with switch statements, making them less flexible in certain situations compared to other control flow statements.
  • It's worth noting that starting from Java 12, switch statements have been enhanced to support switch expressions, which allow the switch statement to be used as an expression that returns a value. This feature offers more flexibility and can result in more concise and readable code in certain situations.
  • Overall, the switch statement in Java is a powerful tool for controlling the flow of a program based on the value of an expression, offering a clear and efficient way to handle multiple branching scenarios.
  • Switch statements can sometimes lead to code duplication, especially when multiple case blocks perform similar actions. It can make the code harder to maintain and debug.
  • While switch statements can be used with String objects since Java 7, they cannot be used directly with other object types. This limitation can make switch statements less useful in scenarios where complex objects need to be compared and evaluated.

Next TopicJava For Loop