The switch
statement is one of the selection statements in Java. It provides a convenient way to select and execute a block of code based on the value of an expression. The switch statement evaluates the expression once and then matches the value of the expression with the values specified in the case labels.
How the switch case works
When the switch
statement is executed, it evaluates the expression inside the switch and compares it with the values specified in the case labels. If a match is found, the corresponding block of code following that case label is executed. If no match is found, the code block following the default label (if present) is executed.
The break statement
By default, after executing a block of code associated with a case label, the switch
statement continues executing the code from the next case label unless a break
statement is encountered. The break
statement is used to exit the switch block and prevent the execution of subsequent case blocks. It is essential to include a break
statement at the end of each case block to avoid unintended fall-through behavior.
The default case
The default
case is optional in a switch statement. It is executed when no match is found between the expression value and the case labels. If a default case is provided, the code block following the default
label will be executed. It is typically used to handle situations where none of the other case conditions are met.
In this tutorial, we’ll show you some examples of how to use the switch
statement in Java.
switch
statement, the if-else
statement is another selection statement that we can use to execute our code conditionally in our Java program.Supported Argument Types
The switch
statement accepts specific types of arguments in Java. Here are the supported argument types:
char
byte
short
int
Character
Byte
Short
Integer
enum
String
String
type arguments to the switch
statement for value comparison.Example 1: Using int Type Argument
Let’s start with an example that demonstrates the usage of the switch
statement with an int
type argument:
IntSwitchStatementExample.java
public class IntSwitchStatementExample {
public static void main(String[] args) {
// Using int type argument in the switch statement
int monthNumber = 6;
switch (monthNumber) {
case 1:
System.out.println("Month number " + monthNumber + " is January");
break;
case 2:
System.out.println("Month number " + monthNumber + " is February");
break;
case 3:
System.out.println("Month number " + monthNumber + " is March");
break;
case 4:
System.out.println("Month number " + monthNumber + " is April");
break;
case 5:
System.out.println("Month number " + monthNumber + " is May");
break;
case 6:
System.out.println("Month number " + monthNumber + " is June");
break;
case 7:
System.out.println("Month number " + monthNumber + " is July");
break;
case 8:
System.out.println("Month number " + monthNumber + " is August");
break;
case 9:
System.out.println("Month number " + monthNumber + " is September");
break;
case 10:
System.out.println("Month number " + monthNumber + " is October");
break;
case 11:
System.out.println("Month number " + monthNumber + " is November");
break;
case 12:
System.out.println("Month number " + monthNumber + " is December");
break;
default:
System.out.println("Month number " + monthNumber + " is an invalid month");
}
}
}
Output:
Month number 6 is June
Example 2: Using String Type Argument
Now, let’s explore an example that showcases the usage of the switch
statement with a String
type argument:
StringSwitchStatementExample.java
public class StringSwitchStatementExample {
public static void main(String[] args) {
// Using String type argument in the switch statement
String day = "Saturday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
System.out.println(day + " is weekday");
break;
case "Saturday":
case "Sunday":
System.out.println(day + " is weekend");
break;
default:
System.out.println(day + " is an invalid day");
}
}
}
Output:
Saturday is weekend
Example 3: Using enum Type Argument
In addition to int
and String
types, the switch
statement can also accept enum
type arguments. Here’s an example:
EnumSwitchStatementExample.java
public class EnumSwitchStatementExample {
public static void main(String[] args) {
// Using enum type argument in the switch statement
Day day = Day.TUESDAY;
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
System.out.println(day + " is a weekday");
break;
case SATURDAY:
case SUNDAY:
System.out.println(day + " is a weekend");
break;
default:
System.out.println(day + " is an invalid day");
}
}
}
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, SATURDAY, SUNDAY
}
Output:
TUESDAY is a weekday
Example 4: Using byte Type Argument
Here is an example that demonstrates the usage of the switch
statement with an byte
type argument:
ByteSwitchStatementExample.java
public class ByteSwitchStatementExample {
public static void main(String[] args) {
byte dayOfWeek = 4;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output:
Thursday
Example 5: Using short Type Argument
This is an example of using an short
type argument:
ShortSwitchStatementExample.java
public class ShortSwitchStatementExample {
public static void main(String[] args) {
short statusCode = 200;
switch (statusCode) {
case 200:
System.out.println("Success");
break;
case 404:
System.out.println("Not Found");
break;
case 500:
System.out.println("Internal Server Error");
break;
default:
System.out.println("Unknown status code");
}
}
}
Output:
Success
Example 6: Nested switch Statements
Nested switch statements allow for more complex control flow and decision-making by nesting one switch statement within another.
int dayOfWeek = 3;
int timeOfDay = 2;
switch (dayOfWeek) {
case 1:
case 2:
case 3:
case 4:
case 5:
switch (timeOfDay) {
case 1:
case 2:
System.out.println("Working hours");
break;
case 3:
case 4:
System.out.println("After-work hours");
break;
default:
System.out.println("Invalid time");
}
break;
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Invalid day");
}
Output:
Working hours
Summary
In this tutorial, we explored the switch
statement in Java and learned how to use it with different argument types. The switch
statement provides a concise way to handle multiple possible values of an expression. By using case labels, you can direct the flow of your code based on specific value matches. Remember to include break
statements to prevent unintended fall-through behavior. The default
case provides a fallback option when no matches are found. With the examples provided, you can now apply the switch statement effectively in your Java programs.
You can find the example codes from this tutorial on GitHub.