Switch Statements in Java
Last Updated : 11 Apr, 2025
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 the 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).
Example: Java program demonstrates how to use the switch statement.
Java // Java program demonstrates how to use switch-case public class Geeks { public static void main(String[] args) { // Replace with desired size (1, 2, 3, 4, or 5) int size = 2; switch (size) { case 1: System.out.println("Extra Small"); break; case 2: System.out.println("Small"); break; case 3: System.out.println("Medium"); break; case 4: System.out.println("Large"); break; case 5: System.out.println("Extra Large"); break; default: System.out.println("Invalid size number"); } } }
Syntax of Java Switch Statement
switch(expression)
{
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
….
….
….
default :
// default Statement
}
Some Important Rules for Java Switch Statements
- Case values must be constants or literals and of the same type as the switch expression.
- Duplicate case values are not allowed.
- The break statement is used to exit from the switch block. It is optional but recommended to prevent fall-through.
- The default case is optional and executes if no case matches the switch expression. It can appear anywhere within the switch block.
Note:
- Starting from Java 7, switch statements can use String type values. They can also handle wrapper classes like Integer, Short, Byte, Long.
- 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.
Flowchart of Switch-Case Statement
This flowchart shows the control flow and working of switch statements:

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: Another example of switch statement in Java to print the day according to their equivalent number.
Java // Java program to Demonstrate Switch Case // with Primitive(int) Data Type public class Geeks { // Main driver method public static void main(String[] args) { int day = 5; String dayString; // Switch statement with int data type switch (day) { // Case case 1: dayString = "Monday"; break; // Case case 2: dayString = "Tuesday"; break; // Case case 3: dayString = "Wednesday"; break; // Case case 4: dayString = "Thursday"; break; // Case case 5: dayString = "Friday"; break; // Case case 6: dayString = "Saturday"; break; // Case case 7: dayString = "Sunday"; break; // Default case default: dayString = "Invalid day"; } System.out.println(dayString); } }
Explanation: 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.
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:
Java // Java Program to Demonstrate Switch Case // with Multiple Cases Without Break Statements // Class public class Geeks { // main driver method public static void main(String[] args) { int day = 2; String dayType; String dayString; // Switch case switch (day) { // Case case 1: dayString = "Monday"; break; // Case case 2: dayString = "Tuesday"; break; // Case case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday"; break; case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; break; case 7: dayString = "Sunday"; break; default: dayString = "Invalid day"; } switch (day) { // Multiple cases without break statements case 1: case 2: case 3: case 4: case 5: dayType = "Weekday"; break; case 6: case 7: dayType = "Weekend"; break; default: dayType = "Invalid daytype"; } System.out.println(dayString + " is a " + dayType); } }
OutputTuesday 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:
Java // Java Program to Demonstrate // Nested Switch Case Statement public class Geeks { // Main driver method public static void main(String[] args) { // Custom input string String Branch = "CSE"; int year = 2; // Switch case switch (year) { // Case case 1: System.out.println( "elective courses : Advance english, Algebra"); // Break statement to hault execution here // itself if case is matched break; // Case case 2: // Switch inside a switch // Nested Switch switch (Branch) { // Nested case case "CSE": case "CCE": System.out.println( "elective courses : Machine Learning, Big Data"); break; // Case case "ECE": System.out.println( "elective courses : Antenna Engineering"); break; // default case // It will execute if above cases does not // execute default: // Print statement System.out.println( "Elective courses : Optimization"); } } } }
Outputelective 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:
Java // Java Program to Illustrate Use of Enum // in Switch Statement public class Geeks { // Enum public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat } // Main driver method public static void main(String args[]) { // Enum Day[] DayNow = Day.values(); // Iterating using for each loop for (Day Now : DayNow) { // Switch case switch (Now) { // Case 1 case Sun: System.out.println("Sunday"); // break statement that hault further // execution once case is satisfied break; // Case 2 case Mon: System.out.println("Monday"); break; // Case 3 case Tue: System.out.println("Tuesday"); break; // Case 4 case Wed: System.out.println("Wednesday"); break; // Case 5 case Thu: System.out.println("Thursday"); break; // Case 6 case Fri: System.out.println("Friday"); break; // Case 7 case Sat: System.out.println("Saturday"); } } } }
OutputSunday 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 1: Writing default in the middle of switch statements:
Java import java.io.*; class Geeks { public static void main (String[] args) { int i=2; switch(i){ default: System.out.println("Default"); case 1: System.out.println(1); break; case 2: System.out.println(2); case 3: System.out.println(3); } } }
Example 2: Writing Default at the Beginning of Switch Statements
Java import java.io.*; class Geeks { public static void main(String[] args) { int i = 5; switch (i) { default: System.out.println("Default"); case 1: System.out.println(1); break; case 2: System.out.println(2); case 3: System.out.println(3); } } }
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
Java import java.io.*; class Geeks { public static void main(String[] args) { int x = 2; switch (x + 1) { case 1: System.out.println(1); break; case 1 + 1: System.out.println(2); break; case 2 + 1: System.out.println(3); break; default: System.out.println("Default"); } } }
Example 2: Case Label Cannot Be Variable
A case label cannot be a variable or variable expression. It must be a constant expression.
Java import java.io.*; class Geeks { public static void main(String[] args) { int x = 2; int y = 1; switch (x) { case 1: System.out.println(1); break; case 2: System.out.println(2); break; case x + y: System.out.println(3); break; default: System.out.println("Default"); } } }
./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 1:
Java public class Geeks { public static void main(String[] args) { Integer age = 25; switch (age) { // Extract primitive value for switch case 25: System.out.println("You are 25."); break; case 30: System.out.println("You are 30."); break; default: System.out.println("Age not matched."); } } }
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 2: Using Character Wrapper
Java public class Geeks { public static void main(String[] args) { Character ch = 'c'; switch (ch) { // Extract primitive value for switch case 'a': System.out.println("You are a."); break; case 'c': System.out.println("You are c."); break; default: System.out.println("Character not matched."); } } }
Exercise
To practice Java switch statements you can visit the page: Java Switch Case statement Practice
Similar Reads
Java if statement
The Java if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not. Example: [GFGTABS] Java // Java program to ill
5 min read
Jump Statements in Java
Jumping statements are control statements that transfer execution control from one point to another point in the program. There are three Jump statements that are provided in the Java programming language: Break statement.Continue statement.Return StatementBreak statement1. Using Break Statement to
5 min read
Enhancements for Switch Statement in Java 13
Java 12 improved the traditional switch statement and made it more useful. Java 13 further introduced new features. Before going into the details of new features, let's have a look at the drawbacks faced by the traditional Switch statement. Problems in Traditional Switch1. Default fall through due t
5 min read
Java if-else Statement
The if-else statement in Java is a powerful decision-making tool used to control the program's flow based on conditions. It executes one block of code if a condition is true and another block if the condition is false. In this article, we will learn Java if-else statement with examples. Example: [GF
4 min read
String in Switch Case in Java
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated
3 min read
Pattern Matching for Switch in Java
Pattern matching for a switch in Java is a powerful feature that was introduced in Java 14. Before this update, a switch expression could only be used to match the type of a value, rather than its actual value. However, by comparing the model, developers can now match the values ââof Strings, enums,
8 min read
Java Break Statement
The Break Statement in Java is a control flow statement used to terminate loops and switch cases. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. Example: [GFGTABS]
3 min read
Record Patten with Switch in Java 19
In this article, we will learn how to use the Record Pattern with Switch statements in Java 19 to create more maintainable, readable code. With the Record Pattern and Switch, we can create cleaner and more efficient code, making our programming experience more productive. As we all know switch state
4 min read
Shift Operator in Java
Operators in Java are used to performing operations on variables and values. Examples of operators: +, -, *, /, >>, <<. Types of operators: Arithmetic Operator,Shift Operator,Relational Operator,Bitwise Operator,Logical Operator,Ternary Operator andAssignment Operator. In this article, w
4 min read
Main thread in Java
Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.When a Java program starts up, one thread begins running i
4 min read