Java Switch Statement

2 Jul 2025 | 9 min read

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.

Let's understand about switch statement in Java in detail.

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.

Points to Remember

  • 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:

Flowchart of 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.

Example of switch Statement

Output:

 20 

Example: Finding Month Using Switch Case

Output:

 7 - July 

Java Program to Check Vowel or Consonant

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

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.

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.

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.

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.

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.

Output:

 You are eligible to vote.

Important Points

  • 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.

Java Switch Case MCQs

1. Which of the following data types can be used as the controlling expression in a switch statement in Java?

  1. int
  2. long
  3. String
  4. All of the above
 

Answer: d)

Explanation: In Java, the switch statement can be used with int, long, and String data types as the controlling expression.


2. What happens if a break statement is omitted in a case block of a switch statement?

  1. The switch statement continues to the next case block.
  2. The switch statement exits the switch block.
  3. The switch statement generates a compilation error.
  4. The behavior is undefined.
 

Answer: a)

Explanation: If a break statement is omitted, the switch statement will continue executing the code in subsequent case blocks until a break statement is encountered or the switch block ends.


3. Which of the following statements is true about fall-through behavior in a switch statement?

  1. Fall-through behavior is an error in Java switch statements.
  2. Fall-through behavior allows the execution of multiple case blocks sequentially until a break statement is encountered.
  3. Fall-through behavior is only allowed in switch statements with a default case.
  4. Fall-through behavior is the default behavior of a switch statement.
 

Answer: b)

Explanation: Fall-through behavior in a switch statement allows the execution of multiple case blocks sequentially until a break statement is encountered or until the end of the switch block is reached.


4. Which of the following statements is true regarding the use of switch statements with String objects in Java?

  1. Switch statements with String objects are not allowed in Java.
  2. Switch statements with String objects can only be used with constant expressions.
  3. Switch statements with String objects are supported in Java starting from version 7.
  4. Switch statements with String objects are less efficient than using if-else statements.
 

Answer: c)

Explanation: Switch statements with String objects are supported in Java starting from version 7.


5. Which of the following is NOT a valid use case for a switch statement in Java?

  1. Menu selection in a command-line application
  2. Handling different HTTP methods in a web server
  3. Evaluating complex conditions based on user input
  4. Implementing a state machine
 

Answer: c)

Explanation: While switch statements are suitable for handling multiple conditions, they are not well-suited for evaluating complex conditions based on user input.


Next TopicJava For Loop