The switch
statement is one of the selection statements in Java. We can use this statement with the break
statement to define and control the flow in our code.
In this tutorial, we’ll show you some examples of how to use the switch
statement in Java.
Info: Except the switch
statement, the if-else
statement is another selection statement that we can use to execute our code conditionally in our Java program.
What You’ll Need
- Oracle JDK 1.7 or above
Supported Argument Types
We can’t pass all data types to switch
statement. The switch
statement only can accept the following types of argument:
char
byte
short
int
Character
Byte
Short
Integer
enum
String
Note: Beginning with Java 7, we can pass String
type argument to switch
statement to compare the value.
Example 1: Using int Type Argument
This is the code snippet of switch
statement with 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
This is the code snippet of switch
statement with 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
This is the code snippet of switch
statement with enum
type argument.
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 weekday");
break;
case SATURDAY:
case SUNDAY:
System.out.println(day + " is weekend");
break;
default:
System.out.println(day + " is an invalid day");
}
}
}
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, SATURDAY, SUNDAY
}
Output:
TUESDAY is weekday
Summary
In this tutorial, we have shown you several examples of using switch
statement with different types of argument such as int
, String
and enum
.
You can get the example codes of this tutorial on GitHub.