![]() |
VOOZH | about |
The switch statement in Java is a multi-way decision statement that executes different blocks of code based on the value of an expression. It provides a cleaner and more readable alternative to long if-else-if ladders.
Small
Note: long is not supported in switch statements.)
switch(expression)
{
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
....
....
....
default :
// default Statement
}
Note:
- Starting from Java 7, switch statements can use String type values. They can also handle wrapper classes like Integer, Short, Byte, Long.
- 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.
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.
Example: Another example of switch statement in Java to print the day according to their equivalent number.
Friday
Explanation: 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.
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.
Example:
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.
Example 1: Writing default in the middle of switch statements:
2 3
Example 2: Writing Default at the Beginning of Switch Statements
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
3
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 errorJava 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.
Example 1:
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).
Example 2: Using Character Wrapper
You are c.