Java Switch Statement
In Java, the switch statement is a control flow statement that allows you to execute one block of code out of many options based on the value of an expression. It works as an alternative to multiple if-else-if statements, making your code cleaner, more readable, and easier to maintain when you have to compare a single variable against multiple constant values.
Syntax of switch Statement
switch (expression) {
case value1:
// Code block for case 1
break;
case value2:
// Code block for case 2
break;
…
default:
// Code block if no cases match
}
Explanation of switch Components
- expression:
This is evaluated once and compared with each case value. It can be of type byte, short, char, int, String, or an enum in Java.
(Since Java 7, String values can also be used in a switch statement.) - case labels:
Each case specifies a possible value that the expression might match. If a match is found, the corresponding block of code executes. - break statement:
It is used to exit the switch block after executing a matching case. Without break, execution will continue into the next case — this is called fall-through behavior. - default case:
This is optional. It runs only if none of the case values match the expression.
Example 1: Simple switch Example
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
case 3:
System.out.println(“Wednesday”);
break;
case 4:
System.out.println(“Thursday”);
break;
case 5:
System.out.println(“Friday”);
break;
case 6:
System.out.println(“Saturday”);
break;
case 7:
System.out.println(“Sunday”);
break;
default:
System.out.println(“Invalid day number!”);
}
}
}
Output:
Wednesday
Explanation:
The value of day is 3, so the program prints “Wednesday”. The break statement prevents the execution from falling into the next case.
Example 2: switch Statement with String
Since Java 7, you can use Strings inside a switch statement.
public class SwitchStringExample {
public static void main(String[] args) {
String browser = “Chrome”;
switch (browser) {
case “Chrome”:
System.out.println(“Launching Chrome Browser…”);
break;
case “Firefox”:
System.out.println(“Launching Firefox Browser…”);
break;
case “Edge”:
System.out.println(“Launching Edge Browser…”);
break;
default:
System.out.println(“Unknown Browser!”);
}
}
}
Output:
Launching Chrome Browser…
Example 3: switch Statement without break (Fall-Through Behavior)
If you omit the break statement, Java continues to execute the next case statements until a break or the end of the switch block is reached.
public class FallThroughExample {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println(“One”);
case 2:
System.out.println(“Two”);
case 3:
System.out.println(“Three”);
default:
System.out.println(“Invalid”);
}
}
}
Output:
Two
Three
Invalid
Explanation:
Since there’s no break after each case, execution “falls through” to the next cases.
Example 4: Enhanced switch Statement (Java 14 and Later)
From Java 14, a new and more expressive switch expression was introduced. It can return values and uses the arrow (→) syntax.
public class EnhancedSwitchExample {
public static void main(String[] args) {
int day = 4;
String dayType = switch (day) {
case 1, 2, 3, 4, 5 -> “Weekday”;
case 6, 7 -> “Weekend”;
default -> “Invalid Day”;
};
System.out.println(“It’s a ” + dayType);
}
}
Output:
It’s a Weekday
Explanation:
The enhanced switch is concise, supports multiple case labels, and can directly return a value, making it perfect for functional-style programming.
Key Points to Remember
- switch can be used with byte, short, char, int, String, and enum types.
- Always use break unless you intentionally want fall-through behavior.
- default is optional but recommended for handling unexpected values.
- Enhanced switch (Java 14+) supports returning values and uses -> syntax.
- switch expressions are more concise and eliminate fall-through issues.
Conclusion
The Java switch statement is a powerful and cleaner alternative to long chains of if-else-if statements, especially when checking the same variable against multiple possible values. With the introduction of enhanced switch expressions in newer Java versions, developers now have a more elegant, functional, and safer way to handle conditional branching in their code.