Java Switch Statement2 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
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 ![]() 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 StatementExampleCompile and RunOutput: 20 Example: Finding Month Using Switch CaseExampleCompile and RunOutput: 7 - July Java Program to Check 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. Important Points
Java Switch Case MCQs1. Which of the following data types can be used as the controlling expression in a switch statement in Java?
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?
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?
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?
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?
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 |
We request you to subscribe our newsletter for upcoming updates.