--- title: Java Switch Statement - Scaler Topics description: Learn about switch case statements in Java by Scaler Topics. Switch case in Java is the comparison between a variable and the subsequent values. author: Manas Gupta category: Java --- :::section{.abstract} Switch statement in Java is used to select one of the many code blocks to be executed. The value of the expression in switch case in java is compared with the values of each case. If there is a match, the associated block of code is executed. Switch case in Java is *fall-through* which means it executes all the cases after the first match if a break statement is not encountered. ![Switch Statement in Java](https://scaler.com/topics/images/Switch-Statement-in-Java-1536x1536.webp) ### Some Important Points to Remember Let’s quickly go through some standard rules for writing switch cases in java. * **No variables:** The case value in switch statement must be a literal or constant. Also, it must be of the same type as switch expression. * **No duplicates:** No two cases in a switch statement should be of same value. Otherwise, compilation error is thrown. * **Allowed Types:** The Java switch expression must be of `int`, `long`, `byte`, `short`, `enums` and `String` type. Primitives are allowed with their wrapper types. * **Optional Break Statement:** Break statement is optional in Java's switch statement. If a case is matched and there is no `break` statement mentioned, subsequent cases are executed until a `break` statement or end of the switch statement is encountered. * **Optional default case:** `default` case value is optional. The `default` statement is meant to execute when there is no match between the values of the variable and the cases. **It can be placed anywhere in the switch block** according to the required result. ::: :::section{.main} ## Syntax ```java switch(expression) { case x: // code block break; // optional case y: // code block break; // optional default: // optional // code block to be executed if no cases match } ``` Let us understand the usage of switch case in Java programmatically. ::: :::section{.main} ## Example Consider the code snippet given below: ```java public class Example { public static void main(String[] args) { // Declaring a variable for switch expression int day = 4; switch (day) { // Case statements case 1: System.out.println("Day 1: Monday"); break; case 2: System.out.println("Day 2: Tuesday"); break; case 3: System.out.println("Day 3: Wednesday"); break; case 4: System.out.println("Day 4: Thursday"); break; case 5: System.out.println("Day 5: Friday"); break; case 6: System.out.println("Day 6: Saturday"); break; case 7: System.out.println("Day 7: Sunday"); break; // Default case statement default: System.out.println("Invalid day"); } } } ``` **Output:** ```plaintext Day 4: Thursday ``` **Explanation:** * The value of the `day` variable is compared with each of the case values. Since `day = 4`, it matches the fourth case value and `Day 4: Thursday` is printed. * The `break` statement in the fourth case breaks out of the switch statement. ::: :::section{.main} ## The break Keyword When the Java program reads the `break` statement it comes out of the switch block immediately with no further comparisions. The execution of any more code within the **switch-case block** is seized and case testing/comparison is stopped. ::: :::section{.main} ## The default Keyword In switch case in java, the default statement is executed when there is no single comparison that checks out which means the value of variable and the case value doesn’t match in of the any cases. In this scenario, suppose the `day` variable is given the value `9` then all the cases will be checked and since equality conditions cannot be achieved with this value, the default statement will get executed at the end. **Example:** ```java public class Example { public static void main(String[] args) { // Declaring a variable for switch expression int day = 4; switch (day) { // Case statements case 1: System.out.println("Day 1:"); break; case 2: System.out.println("Day 2:"); break; case 3: System.out.println("Day 3:"); break; // Default case statement default: System.out.println("Invalid day"); } } } ``` **Output:** ```plaintext Invalid day ``` ::: :::section{.main} ## Java Nested Switch Statement *A nested switch case in java statement refers to a scenario where a switch statement is used inside another switch statement.* Basically, **it is switch statement within a switch statement.** **Example:** ```java // Java Program to demonstrate the use of Java Nested Switch public class NestedSwitchExample { public static void main(String args[]) { int year = 3, semester = 2; switch (year) { case 1: switch (semester) { case 1: System.out.println("Total semesters passed: 1"); break; case 2: System.out.println("Total semesters passed: 2"); break; } break; case 2: switch (semester) { case 1: System.out.println("Total semesters passed: 3"); break; case 2: System.out.println("Total semesters passed: 4"); break; } break; case 3: switch (semester) { case 1: System.out.println("Total semesters passed: 5"); break; case 2: System.out.println("Total semesters passed: 6"); break; } break; case 4: switch (semester) { case 1: System.out.println("Total semesters passed: 7"); break; case 2: System.out.println("Total semesters passed: 8"); break; } break; } } } ``` **Output:** ```plaintext Total semesters passed: 6 ``` ::: :::section{.summary} ## Conclusion * The Switch case in Java is the comparison between a variable and its possible values. * Break statement is used to come out of the switch block immediately without any further comparisons. * Default statement is executed only when there is no case with value same as the variable value. * Switch case is called a fall-through statement when we miss out break statements in switch cases. * We can nest one switch statement within another switch statement with independent variable values. * We can use enum and wrapper classes in switch statements in Java. :::