The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.
It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The expression can be of type byte , short , char , int , long , enums, String , or wrapper classes ( Integer , Short , Byte , Long ).
Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types ( Enums in java), the String class, and Wrapper classes.
switch(expression) < case value1 : // Statements break; // break is optional case value2 : // Statements break; // break is optional . . . default : // default Statement >
Small
Note: Starting from Java 7, switch statements can use String type values. They can also handle wrapper classes like Integer , Short , Byte , Long .
This flowchart shows the control flow and working of switch statements:
Note: Java switch statement is a fall through statement that means it executes all statements if break keyword is not used, so it is highly essential to use break keyword inside each case.
Consider the following Java program, it declares an int named day whose value represents a day(1-7). The code displays the name of the day, based on the value of the day, using the switch statement.
Friday
A break statement is optional. If we omit the break, execution will continue into the next case.
It is sometimes desirable to have multiple cases without “ break ” statements between them. For instance, let us consider the updated version of the above program, it also displays whether a day is a weekday or a weekend day.
Tuesday is a Weekday
We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch . Since a switch statement defines its block, no conflicts arise between the case constants in the inner switch and those in the outer switch.
elective courses : Machine Learning, Big Data
Enums in Java are a powerful feature used to represent a fixed set of constants. They can be used in switch statements for better type safety and readability.
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
The default case in a switch statement specifies the code to run if no other case matches. It can be placed at any position in the switch block but is commonly placed at the end.
Default 1
Case labels and switch arguments can be constant expressions. The switch argument can be a variable expression but the case labels must be constant expressions.
Example: Using Variable in Switch Argument
A case label cannot be a variable or variable expression. It must be a constant expression.
./GFG.java:16: error: constant expression required case x+y: ^ 1 error
Java allows the use of wrapper classes ( Integer , Short , Byte , Long , and Character ) in switch statements. This provides flexibility when dealing with primitive data types and their corresponding wrapper types.
You are 25.
Note: Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic (unless you’re using a less common technique called fall-through).
You are c.
To practice Java switch statements you can visit the page: Java Switch Case statement Practice
Switch statements in Java are control flow structures that allow you to execute specific blocks of code based on the value of a single expression. They can be considered an alternative to if-else-if statements and are useful for handling multiple conditions in a clean and readable manner.
To use switch statement in Java, you can use the following syntax:
switch (expression)
case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
// … more cases
default:
// code to execute if none of the above cases match
>
No, you can not pass NULL to a switch statement as they require constant expression in its case.
No, switch statements build a control flow in the program, so it can not go back after exiting a switch case.