The switch statement Java is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes. Switch statement is the alternate the of if else if ladder statement.
Here is the table content of the article will we will cover this topic.
1. switch statement in Java?
2. Rules of the switch statement Java
3. Nested switch statements in Java?
4. Does Java switch multiple cases?
5. switch case without a break statement?
6. switch case with char?
7. switch case with wrapper classes?
8. Why does float values are not allowed in the switch statement?
9. Why the break statement is used in the switch statement?
The switch statement executes one block of the statement from multiple blocks of statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice.
switch(variable/expression) { case value1 : // code inside the case value1 break; // optional case value2 : // code inside the case value2 break; // optional . . . default : // code inside the default case . }
Rules of the Switch statement Java
- An expression can be of byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. You can place a variable or an expression in between brackets of switch().
- You can create any number of cases in switch statement.
- The case value must be literal or constant. The case value type should be a type of expression.
- Each case should be unique. If you create duplicate case value, it will throw compile-time error.
- Each case has a break statement which is optional. It is used inside the switch to terminate a statement sequence and jumps the control after the switch expression.
- If a break statement is not used in any case the execution will continue into the next case until a break statement is reached.
- In the switch, we can have an optional default case that must be placed at the end of the switch statement. If none of the cases are true, then this default case will be executed. The default case doesn’t need break statement.

public class ExampleOfSwitch { public static void main(String[] args) { //Declaring a variable for switch expression int noOfDays = 28; switch(noOfDays) //Switch expression { //Case statements case 28: System.out.println("Only February month has 28 days"); break; case 30: System.out.println("April, June, August, October, December has 30 days"); break; case 31: System.out.println("January, March, May, July, September, November has 31 days"); break; //Default case statement default: System.out.println("Please enter valid no of days"); } } }
Output: Only February month has 28 days
In this example, we want to print the name of months based on their days. Firstly, we declared a variable is using in switch expression. The switch expression variable is a type of int. So, we created three cases of type int. Each case has different codes according to our requirements. We also provide a default case at the end of the switch if the user enters any wrong input which doesn’t match with any case then the default case will execute.
1. Quiz, Read the below code and do answer.
Click on anyone to know the answer.