Java Switch Statement
In this tutorial, we are going to discuss the Java Switch Statement, which is another type of control flow statement in Java. This statement allows us to select one of many code blocks to be executed. It's a multi-way branch statement that provides an easy way to dispatch execution to different parts of the code based on the value of an expression.
Prerequisites
Before we delve into Java Switch Statement, make sure you have a basic understanding of Java syntax, variables, and data types.
What is a Switch Statement?
In Java, a switch statement tests a variable for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Here is the general form of a switch statement:
switch(expression) {
case constant :
// Statements
break;
/* You can have any number of case statements */
default :
// Statements
}
Here, the switch
, case
, and default
keywords are used. The switch
statement evaluates the expression
, then according to the expression's
value, it runs the corresponding case
block. If none of the case
matches, the default
block is executed.
The break
keyword is used to exit the switch case. If the break statement is not used, the switch case will continue to test for matches with the remaining cases.
Example of a Switch Statement
Let's look at a simple example:
int day = 3;
switch(day) {
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
default :
System.out.println("Invalid Day");
}
In the above code, the switch
statement is testing the variable day
. Since the value of day
is 3
, it matches with case 3
and "Wednesday" is printed to the console. If day
was 5
, there is no matching case, so the default
case would be executed and "Invalid Day" would be printed to the console.
Enhanced Switch Statement in Java 12
Java 12 introduced an enhanced switch statement, which can be used as either a statement or an expression. The new switch statement/ expression is simplified to eliminate the fall-through and allows us to directly assign the result of the expression to a variable.
Here is an example of it:
int day = 3;
String dayOfWeek = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid day";
};
System.out.println(dayOfWeek);
In this example, the switch
expression is used to assign a value directly to the dayOfWeek
variable. The arrow ->
is used to indicate the value that should be returned when the case is matched.
Conclusion
The switch
statement in Java is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. It is a more readable form of multiple if..else if
statements. By understanding how to use the switch
statement effectively, you can make your Java code more efficient and easier to read. Remember to always include a default
case in your switch
statements to handle unexpected values.