Switch Statements in Java

The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.

It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The expression can be of type byte , short , char , int , long , enums, String , or wrapper classes ( Integer , Short , Byte , Long ).

Note: 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.

Syntax:

switch(expression) < case value1 : // Statements break; // break is optional case value2 : // Statements break; // break is optional . . . default : // default Statement >

Example: Size Printer Example


Output
Small

Some Important Rules for Java Switch Statements

Note: Starting from Java 7, switch statements can use String type values. They can also handle wrapper classes like Integer , Short , Byte , Long .

Flowchart of Switch-Case Statement

This flowchart shows the control flow and working of switch statements:

switch statement flowchart in java

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: Finding Day

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.


Output
Friday

Break in switch case Statements

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.

Example: Switch statement program without multiple breaks


Output
Tuesday is a Weekday

Java Nested Switch Statements

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: Nested Switch Statement


Output
elective courses : Machine Learning, Big Data

Java Enum in Switch Statement

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.

Example: Use of Enum in Switch


Output
Sunday Monday Tuesday Wednesday Thursday Friday Saturday

Default statement in Java Switch Case

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: Writing default in the middle of switch statements:


Output

Example: Writing Default at the Beginning of Switch Statements


Output
Default 1

Case label variations

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


Output

Example: Case Label Cannot Be Variable

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 error 

Java Wrapper in Switch Statements

Java 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: Using Wrapper Classes


Output
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: Using Character Wrapper


Output
You are c.

Exercise:

To practice Java switch statements you can visit the page: Java Switch Case statement Practice

Conclusion

Switch statements in Java are control flow structures that allow you to execute specific blocks of code based on the value of a single expression. They can be considered an alternative to if-else-if statements and are useful for handling multiple conditions in a clean and readable manner.

Java Switch Statements- FAQs

How to use switch statements in Java?

To use switch statement in Java, you can use the following syntax:

switch (expression)
case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
// … more cases
default:
// code to execute if none of the above cases match
>

Can we pass null to a switch?

No, you can not pass NULL to a switch statement as they require constant expression in its case.

Can you return to a switch statement?

No, switch statements build a control flow in the program, so it can not go back after exiting a switch case.