Java Switch StatementLast 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 StatementThere are several rules and constraints of Java switch statement. Some of them are as follows:
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 StatementHere is the syntax of switch statement: Flowchart of Switch StatementThe following images demonstrates the working flow of a switch statement: ![]() 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 StatementPractice the following examples to understand the concept of switch statement in Java: Example 1: Simple Switch StatementLet us take an example to demonstrate the working of the switch statement in Java. ExampleCompile and RunOutput: 20 Example 2: Finding Month using switch StatementLet us take an example to demonstrate how to find months using the switch statement in Java. ExampleCompile and RunOutput: 7 - July Example 3: Checking Vowel or ConsonantIf the character is A, E, I, O, or U, it is a vowel; otherwise, consonant. It is not case-sensitive. ExampleCompile and RunOutput: Vowel Java Switch Statement is a fall-throughThe Java switch statement is fall-through. It means it executes all statements after the first match if a break statement is not present. ExampleCompile and RunOutput: 20 30 Not in 10, 20 or 30 Java Switch Statement with StringSince Java 7, Java allows us to use strings in switch expressions. The case statement should be a string literal. ExampleCompile and RunOutput: Your Level is: 3 Java Nested Switch StatementWe can use a switch statement inside another switch statement in Java. It is known as a nested switch statement. ExampleCompile and RunOutput: Data Communication and Networks, MultiMedia Using enum in Switch StatementJava 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. ExampleCompile and RunOutput: Sunday Monday Tuesday Wednesday Thursday Friday Saturday Java Wrapper in Switch StatementJava allows us to use four wrapper classes -Byte, Short, Integer, and Long - in the switch statement. ExampleCompile and RunOutput: You are eligible to vote. Features and Limitations of Java Switch StatementThere are several features and limitations of the Java Switch Statament. Some of them are as follows:
Next TopicJava For Loop |
We request you to subscribe our newsletter for upcoming updates.