switch Keyword Usage and Examples
Learn how to use the Java switch keyword to control flow in your programs. Covers syntax, examples, fall-through behavior, and best practices for using switch statements effectively.
This reference explains switch Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
switch Keyword in Java
The switch keyword in Java is used for decision-making. It's an alternative to writing many if-else-if statements, and it allows you to test a single expression against multiple possible values. When readability and clarity matter, switch often makes your code cleaner and more elegant.
Basic Syntax of switch
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// default block
}
Here’s what each part means:
- expression: A variable or value evaluated once, usually
int,char,String(since Java 7), orenum. - case: Each
caseis checked against the value of the expression. - break: Exits the switch block. Without it, execution "falls through" to the next case.
- default: Optional. Executes if no match is found.
Example 1: Integer-Based switch
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;
default:
System.out.println("Another day");
}
Wednesday
Since day is 3, the third case matches and prints "Wednesday". The break prevents the next cases from being executed.
Example 2: What Happens Without break?
int rating = 4;
switch (rating) {
case 5:
System.out.println("Excellent");
case 4:
System.out.println("Good");
case 3:
System.out.println("Fair");
default:
System.out.println("Needs improvement");
}
Good
Fair
Needs improvement
Without break, the switch statement "falls through" and executes all subsequent cases. That’s why it prints everything starting from case 4.
Example 3: Using String in switch (Java 7+)
String role = "admin";
switch (role) {
case "admin":
System.out.println("Welcome Admin");
break;
case "editor":
System.out.println("Welcome Editor");
break;
default:
System.out.println("Role not recognized");
}
Welcome Admin
Java allows String types in switch statements starting with Java 7, making it useful for text-based conditions.
Example 4: switch with char
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done");
break;
case 'C':
System.out.println("You passed");
break;
default:
System.out.println("Invalid grade");
}
Well done
Example 5: switch with enum
enum Direction {
NORTH, SOUTH, EAST, WEST
}
Direction dir = Direction.EAST;
switch (dir) {
case NORTH:
System.out.println("You are heading North");
break;
case EAST:
System.out.println("You are heading East");
break;
}
You are heading East
enum types make switch more expressive and type-safe, often used in game dev or control systems.
Best Practices for switch
- Always include
breakunless fall-through is intentional. - Use
defaultto handle unexpected input. - Consider using
enumfor finite state values to improve readability and safety. - Be cautious when using
String—it’s case-sensitive.
When Not to Use switch
If you need to compare ranges or perform more complex conditions, if-else-if blocks may be more appropriate.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.