Java switch Statement: The If Else condition allows us to choose between TRUE or FALSE. When there are more than two options, we simply use Nested If. Say, What if we have ten alternatives to choose from? if we use Nested If in this situation, then programming logic will be difficult to understand.
The Else if and switch statements in this Programming can handle these problems effectively. We discussed the Else If Statement in our previous post, so let us explore the switch case here.
The working functionality of the switch case in Java programming is almost the same as the If condition. As we said before, it may have n number of cases. So, it compares the expression value with the values assigned in the case block.
If both the values expression value and case value match, then the code block in that switch case statement will execute. Let us see the syntax of it for better understanding.
Java switch case syntax
The syntax of the switch statement in this Programming language is as follows:
Switch (expression) {
Case Option 1:
//Execute when the expression result match Option 1
break;
Case Option 2:
//Execute when the expression result match Option 2
break;
......
Case Option N:
//Execute these when the result of expression match Option N
break;
Default:
/*Execute these when the result of expression
Not matching with any Option */
brea;
}
The detailed explanation behind this Java switch case statement syntax is:
- The expression value should be either integer or characters (We can write the expression as n/2 also, but the result should be an integer or convertible integer).
- It allows us to add a default statement. If the variable value does not match any of the switch case expressions, then the code in the optional default statement will execute.
- The break clause is useful to come out of it. Otherwise, all the code in the condition is executed. Whenever a break is encountered, the execution flow would directly come out of the case. Please refer Java Break Statement article to understand this keyword perfectly.
Java switch case Flow Chart
The following picture will show you the flow chart behind this switch case Statement.

The execution flow of the switch statement or case is:
- If Case = Option 1, then STATEMENT1 is executed, followed by a break statement to exit.
- If Case = Option 2, then STATEMENT2 is executed, followed by a break to exit.
- When Case = Option 3, then STATEMENT3 will execute, followed by the statement to exit.
- If Option 1, Option 2, and Option 3 Fail, then Default STATEMENT is executed, followed by a break statement to exit from it.
Java switch case example
This program allows the user to enter two integer values. It also allows selecting any Java Arithmetic Operators to perform arithmetic operations.
In this Java switch case example, the first three lines of code include the println, allowing the user to enter two integer values. Then we assign the user entered values to already declared a variable called number1 and number2.
The next println will allow the User to a single character (it should be any Arithmetic operator). Then we assign the user-entered character to already declared a variable called an operator.
Next, we use the switch case statement with the operator as an option. If the user inputs + as an operator, the code inside the + block will be printed.
If the user enters – as an operator, the below statement will print. When the user gives *, the following code will print.
If the user enters / as an operator, the code inside the / section will print. And if the user provides % as an operator, the println inside the % section will print.
And if the user entered operator (character) is not in any of the above (operator not equal to +, -, *, / or %), the following default statement in the Java switch case will print.
TIP: Please refer to the Java If Condition and Java ternary operator articles on the Java tutorial page.
package ConditionalStatements;
import java.util.Scanner;
public class Switch {
private static Scanner sc;
public static void main(String[] args) {
int number1, number2;
char operator;
sc = new Scanner(System.in);
System.out.println(" Please Enter two values to perform Arithmetic Operations ");
number1 = sc.nextInt();
number2 = sc.nextInt();
System.out.println(" Please select any ARITHMETIC OPERATOR You wish!\n");
operator = sc.next().charAt(0);
switch (operator) {
case '+':
System.out.format("Addition of two numbers is: %d", number1 + number2);
break;
case '-':
System.out.format("Subtraction of two numbers is: %d", number1 - number2);
break;
case '*':
System.out.format("Multiplication of two numbers is: %d", number1 * number2);
break;
case '/':
System.out.format("Division of two numbers is: %d", number1 / number2);
break;
case '%':
System.out.format("Module of two numbers is: %d", number1 % number2);
break;
default:
System.out.println("You have entered the Wrong operator\n");
System.out.println("Please enter the Correct operator such as +, -, *, /, %%");
break;
}
}
}
OUTPUT 1: Let us enter the * operator as a switch case expression value and enter number1 = 10, number 2 =20.

Let us enter the wrong operator to check the default value.
Please Enter two values to perform Arithmetic Operations
40 60
Please select any ARITHMETIC OPERATOR You wish!
6
You have entered the Wrong operator
Please enter the Correct operator such as +, -, *, /, %%
Java switch case string example
In this program, we are using the string data as an option. In this Java switch case example, we divide the code using Object-Oriented Programming. First, we will create a class that holds methods to do this. Refer to the Java Else If statement and Java Nested If statement articles
package ConditionalStatements;
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args) {
String month, Message;
sc = new Scanner(System.in);
System.out.println("Please Enter any Month Name");
month = sc.next();
MonthClass swt = new MonthClass();
Message = swt.MonthNum(month);
System.out.println(Message);
}
}
Within the Main program of this switch case example, we will create an instance of the above-specified class and call the methods.
package ConditionalStatements;
public class MonthClass {
public String MonthNum(String month) {
String Message;
switch (month.toLowerCase()) {
case "january":
Message = "You have entered January and Month Number = 1";
break;
case "february":
Message = "You have entered February and Month Number = 2";
break;
case "march":
Message = "You have entered March and Month Number = 3";
break;
case "april":
Message = "You have entered April and Month Number = 4";
break;
case "may":
Message = "You have entered May and Month Number = 5";
break;
case "june":
Message = "You have entered June and Month Number = 6";
break;
case "july":
Message = "You have entered July and Month Number = 7";
break;
case "august":
Message = "You have entered August and Month Number = 8";
break;
case "september":
Message = "You have given September and Month Number = 9";
break;
case "october":
Message = "You have given October and Month Number = 10";
break;
case "november":
Message = "You have given November and Month Number = 11";
break;
case "december":
Message = "You have given December and Month Number = 12";
break;
default:
Message = "Please Provide the Proper Month name";
break;
}
return Message;
}
}
OUTPUT 1: Let us provide March as the month name.
Please Enter any Month Name
march
You have entered March and Month Number = 3
Let us give the wrong month name to check the default value.
Please Enter any Month Name
mar
Please Provide the Proper Month name
Java switch case multiple values
Similar to an else if statement, there are situations where we must use a switch statement with multiple values. For example, in the following program, each switch case checks the user’s activities.
If the activity matches walking, running, cycling, or gym? the user’s lifestyle is active and healthy. If the activity is meditation or yoga. The user has a healthy, relaxed lifestyle. If the case is system gaming or watching TV? He leads a bad lifestyle.
If none of the above cases match, the Javac compiler moves to the default statement and prints the Worst lifestyle as output.
public class example {
public static void main(String[] args) {
String activities = "running";
switch (activities) {
case "walking", "running", "cycling", "gym":
System.out.println("Active and Healthy Lifestyle");
break;
case "meditation", "yoga":
System.out.println("Healthy Relaxed Lifestyle");
break;
case "system_gaming", "watching_tv":
System.out.println("Bad Lifestyle");
break;
default:
System.out.println("Worst Lifestyle");
}
}
}
Active and Healthy Lifestyle
Java switch case enum
The switch case also allows us to use an enum (enumerator), and we can say it provides the cleanest approach to writing case statements. First, we must declare an enum with the required conditions. Here, we should not use double quotes to mention beach, Mountains, or city. Next, we can use those enum values inside the case expression.
public class example {
enum destination {
MOUNTAINS, BEACH, CITY
}
public static void main(String[] args) {
destination dest = destination.MOUNTAINS;
switch (dest) {
case BEACH:
System.out.println("Visit Goa or Pondi");
break;
case MOUNTAINS:
System.out.println("Visit Himachal Pradesh and Leh");
break;
case CITY:
System.out.println("Visit Mumbai and Hyderabad");
break;
default:
System.out.println("Visit Delhi");
}
}
}
Visit Himachal Pradesh and Leh
Java switch case without break (arrow syntax vs colon)
In older versions, the break statement helps to stop execution at the current stage and makes the Javac compiler jump out of the switch statement. However, it is not mandatory to use the break statement inside a switch statement; it won’t throw an error if we don’t use it.
In the following example, we used the above example but removed the break statements in each switch case. The Javac compiler checks the first expression; it is false. So, it moves to MOUNTAINS, and the expression is true; it prints the message inside it.
As there is no break statement to exit, it moves to the next case and prints the statement inside it. Then moves to default and prints the message in it. Basically, it prints every statement inside the switch case.
public class example {
enum destination {
MOUNTAINS, BEACH, CITY
}
public static void main(String[] args) {
destination dest = destination.MOUNTAINS;
switch (dest) {
case BEACH:
System.out.println("Visit Goa or Pondi");
case MOUNTAINS:
System.out.println("Visit Himachal Pradesh and Leh");
case CITY:
System.out.println("Visit Mumbai and Hyderabad");
default:
System.out.println("Visit Delhi");
}
}
}
Visit Himachal Pradesh and Leh
Visit Mumbai and Hyderabad
Visit Delhi
Arrow syntax vs. colon
From Java 26, switch case can use arrow syntax to remove the break statement completely. It does not require a break or traditional colon syntax. By far, it is the simplest and cleanest approach to writing the switch statement.
public class example {
enum destination {
MOUNTAINS, BEACH, CITY
}
public static void main(String[] args) {
destination dest = destination.BEACH;
switch (dest) {
case BEACH ->
System.out.println("Visit Goa");
case MOUNTAINS ->
System.out.println("Visit Leh");
case CITY ->
System.out.println("Visit Mumbai");
default ->
System.out.println("Visit Delhi");
}
}
}
Visit Goa
Java switch case null pointer exception
In older versions of switch case that use the colon syntax and the break statement, it will throw a Null pointer exception NullPointerException when we pass a null value to the switch case.
public class example {
public static void main(String[] args) {
String destination = null;
switch (destination) {
case "Goa":
System.out.println("Enjoy Beach and Pubs");
break;
case "Ladak":
System.out.println("Enjoy Trekking and Snow");
break;
default:
System.out.println("Visit Delhi");
break;
}
}
}
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "destination" is null at example.main(example.java:8)
To deal with a NullPointerException, we must use the Java if else statement to check for null inputs. However, with the modern arrow syntax, we can display a message for null values.
public class example {
public static void main(String[] args) {
String destination = null;
switch (destination) {
case null -> System.out.println("Enter a non-null Destination");
case "Goa" -> System.out.println("Enjoy Beach and Pubs");
case "Ladakh" -> System.out.println("Enjoy Trekking and Snow");
default -> System.out.println("Visit Delhi");
}
}
}
Enter a non-null Destination
Java switch case multiple conditions
In older versions, we have to use the if-else statement to check multiple conditions inside the switch case expression. However, with the arrow syntax, we can use the “case when” approach to check multiple conditions inside a switch statement.
In the following program, the first switch case expression checks whether the destination equals Beach and the budget is greater than 100. If true, it suggests Hawaii beaches. Here, it checks multiple conditions, and both expressions are combined by the Java logical operator (&&).
public class example {
public static void main(String[] args) {
String destination = "Beach";
int budget = 1500;
String choice = switch (destination) {
case String d when d.equals("Beach") && budget > 800 ->
"Visit Hawaii Beaches and enjoy water sports!";
case String d when d.equals("Beach") && budget <= 800 ->
"Visit South Carolina and enjoy Beaches.";
case String d when d.equals("Mountains") && budget > 500 ->
"Visit Colorado and enjoy the Rocky Mountains.";
case String d when d.equals("Mountains") && budget <= 500 ->
"Visit Gatlinburg and visit a national park.";
default -> "Explore the city nightlife.";
};
System.out.println("Destination: " + destination);
System.out.println("Budget: $" + budget);
System.out.println("Recommendation: " + choice);
}
}
Destination: Beach
Budget: $1500
Recommendation: Visit Hawaii Beaches and enjoy water sports!