Java Switch Statement
The switch
statement in Java is a control flow statement that allows the execution of one block of code among multiple alternatives. It provides a more readable and efficient way of writing a series of if-else
statements when comparing the same expression against different values.
Usage
The switch
statement evaluates an expression, matches the expression's value against a series of case
labels, and executes the block of code associated with the matching case
. If no case
matches, the default
block is executed, if it is present.
Syntax
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
// more cases...
default:
// default code block
}
- expression: A variable or expression that is evaluated once.
- value1, value2, ...: Constant expressions that the
expression
is compared against. - break: Terminates the
switch
block. Without it, execution falls through to subsequent cases. - default: Optional block executed if no
case
matches.
Examples
Example 1: Basic switch
Statement
public class SwitchExample {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
System.out.println("Not in range");
}
}
}
In this example, the switch
statement evaluates the variable number
. Since number
equals 2, the output will be "Two". The break
statement prevents fall-through to subsequent cases.
Example 2: switch
with Strings
public class SwitchStringExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("Fruit is Apple");
break;
case "Banana":
System.out.println("Fruit is Banana");
break;
default:
System.out.println("Unknown fruit");
}
}
}
This example demonstrates the use of a switch
statement with a String
. The switch
checks the value of fruit
, and since it is "Apple", the output will be "Fruit is Apple".
Example 3: switch
without break
public class SwitchFallThroughExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
case 2:
case 3:
System.out.println("Weekday");
break;
case 4:
case 5:
System.out.println("Weekend");
break;
default:
System.out.println("Invalid day");
}
}
}
In this example, multiple case
labels are used to execute the same block of code. The output will be "Weekday" because day
is 3, and the break
statement prevents execution from falling through to the "Weekend" cases.
Tips and Best Practices
- Use
break
Statements: Always includebreak
statements unless intentional fall-through is desired. This prevents unintended execution of subsequent cases. - Default Case: Include a
default
case to handle unexpected values and improve code robustness. - Expression Types: The
switch
expression can be of typebyte
,short
,int
,char
,String
, or an enum. - Avoid Complex Logic: Use
switch
for simple scenarios with discrete values. For complex conditions, consider usingif-else
statements. - Java 12+ Enhancements: In Java 12 and later, switch expressions allow returning values and using the
yield
keyword for more concise code.