VOOZH about

URL: https://www.geeksforgeeks.org/java/switch-statement-in-java/

⇱ Switch Statements in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Switch Statements in Java

Last Updated : 16 May, 2026

The switch statement in Java is a multi-way decision statement that executes different blocks of code based on the value of an expression. It provides a cleaner and more readable alternative to long if-else-if ladders.

  • Supports data types such as byte, short, char, int, enum, String, and wrapper classes.
  • Improves code readability and structure when handling multiple conditions.

Output
Small

Note: long is not supported in switch statements.)

Syntax

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:

👁 switch_conditional_statement

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.


Output
Friday

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.


Output
Tuesday is a Weekday

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:


Output
elective courses : Machine Learning, Big Data

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:


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


Output
2
3

Example 2: 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
3

Example 2: 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

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:


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


Output
You are c.
Comment
Article Tags: