Java Switch
A Java switch statement chooses one block of code to run based on the value of one expression. It is useful when you are comparing the same variable against several exact values.
You could write the same logic with many if and else if statements, but switch often reads more clearly for menus, commands, weekdays, grades, and other fixed choices.
Basic Syntax
A switch starts with the value to check. Each case is one possible matching value, and default runs when none of the cases match.
public class Main {
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;
default:
System.out.println("Unknown day");
}
}
}
Output:
Wednesday
How It Works
Java compares day with each case value. Because day is 3, the code under case 3 runs and prints Wednesday.
The break statement exits the switch. Without it, Java continues into the next case after a match, which is called fall-through. Fall-through is sometimes useful, but beginners should normally include break unless they intentionally want the next case to run too.
The Default Case
The default case is like the final else in an if statement. It gives your program a safe response when the value does not match any listed case.
public class Main {
public static void main(String[] args) {
int choice = 4;
switch (choice) {
case 1:
System.out.println("Start game");
break;
case 2:
System.out.println("Load game");
break;
case 3:
System.out.println("Show settings");
break;
default:
System.out.println("Please choose 1, 2, or 3.");
}
}
}
Output:
Please choose 1, 2, or 3.
Here, choice is 4, so none of the numbered cases match. Java runs the default block instead.
Switch With Strings
A switch can work with several value types, including int, char, and String. With strings, the text must match exactly, including spelling and uppercase or lowercase letters.
public class Main {
public static void main(String[] args) {
String command = "pause";
switch (command) {
case "start":
System.out.println("Starting...");
break;
case "pause":
System.out.println("Paused");
break;
case "stop":
System.out.println("Stopped");
break;
default:
System.out.println("Unknown command");
}
}
}
Output:
Paused
Grouping Cases
If several cases should run the same code, place them one after another before the shared statements. This is an intentional use of fall-through.
public class Main {
public static void main(String[] args) {
String day = "Saturday";
switch (day) {
case "Saturday":
case "Sunday":
System.out.println("Weekend");
break;
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
System.out.println("Weekday");
break;
default:
System.out.println("Not a valid day");
}
}
}
Output:
Weekend
When To Use Switch
- Use
switchwhen one value is being compared with many exact options. - Use
ifandelse ifwhen you need ranges, such asscore >= 90, or more complex conditions. - Use
defaultwhen unmatched values need a clear response. - Remember that
breakprevents accidental fall-through.
Takeaway: a Java switch keeps multi-choice control flow organized when each choice is based on a specific matching value.
