switch Keyword in Java
The switch
keyword in Java is used to execute one block of code among many alternatives. It is a control statement that allows the variable to be tested for equality against a list of values, each with its own block of code.
Usage
The switch
statement provides a cleaner and more readable way to handle multiple conditional branches compared to using multiple if-else
statements.
Syntax
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
// more cases...
default:
// default code block
}
expression
: The variable or expression whose value is compared against the case values.case value
: A constant value that the expression is compared to. If they match, the corresponding code block is executed.break
: Terminates theswitch
statement, preventing fall-through.default
: Executes if no case value matches the expression.
Examples
Example 1: Basic Usage
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");
}
}
}
In this example, the variable day
is compared against the case values. Since day
is 3, the output will be "Wednesday".
Example 2: Using 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;
case "Cherry":
System.out.println("Fruit is Cherry");
break;
default:
System.out.println("Unknown fruit");
}
}
}
This example demonstrates how to use a switch
statement with String
values. Since fruit
is "Apple", the output will be "Fruit is Apple".
Example 3: Fall-Through Behavior
public class SwitchFallThroughExample {
public static void main(String[] args) {
int num = 2;
switch (num) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
break;
default:
System.out.println("Default case");
}
}
}
In this example, the absence of break
statements results in fall-through behavior. Since num
is 2, the output will be:
Two
Three
Default case
Tips and Best Practices
- Use
break
Statements: Always usebreak
statements to prevent fall-through unless explicitly desired. - Default Case: Include a
default
case to handle unexpected values. - Constant Values: Ensure that case values are constants or literals.
- Type Compatibility: The
switch
expression can be of typesbyte
,short
,char
,int
,String
, or an enum type. Ensure the case values are compatible with the type of the expression. - Readable Code: Use
switch
statements to make your code more readable and maintainable compared to multipleif-else
statements. - Java 12 Enhancements: As of Java 12, you can use switch expressions to return values directly from cases, simplifying the syntax and enhancing code readability.
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class SwitchEnumExample {
public static void main(String[] args) {
Day day = Day.WEDNESDAY;
switch (day) {
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
}
}
}
Using enums in switch
statements can enhance code readability and type safety.