switch-case in Java
In this lesson: Learn switch-case in Java — break, default, strings in switch and new arrow syntax — examples with output. In Hindi & English.
What is switch-case
A switch-case compares one value against several fixed options and runs the matching one. When you would otherwise write a long else if ladder checking the same variable, a switch is cleaner and easier to read.
A basic 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("Other day");
}
The switch checks day against each case. It matches case 3, prints Wednesday, and break stops it there.
Why break matters
int n = 1;
switch (n) {
case 1: System.out.println("One");
case 2: System.out.println("Two"); // no break above!
default: System.out.println("Done");
}
Two
Done
Without break, Java "falls through" and runs the following cases too. That is why each case usually ends with break — to stop after the match.
The default case
char grade = 'X';
switch (grade) {
case 'A': System.out.println("Excellent"); break;
case 'B': System.out.println("Good"); break;
default: System.out.println("Invalid grade");
}
default runs when nothing else matches — like the final else. It is good practice to always include it to handle unexpected values.
Fall-through on purpose
int month = 4;
switch (month) {
case 3: case 4: case 5:
System.out.println("Spring"); break;
case 6: case 7: case 8:
System.out.println("Summer"); break;
}
Sometimes fall-through is useful: stacking cases with no code between them lets several values share one action. Here months 3, 4 and 5 all print "Spring".
The modern switch
int day = 3;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Other";
};
System.out.println(name);
Newer Java offers an arrow form with no fall-through and no break. It can even return a value directly, making the code shorter and safer.
Common mistakes
- Forgetting
break. In the classic switch, missing break causes fall-through. - No default case. Always handle unmatched values with
default. - Using ranges. A case must be one exact value —
case > 5is invalid; use if-else for ranges. - Wrong type. Cases must match the switch variable's type (int, char, String, enum).
Summary
- switch-case compares one value to several fixed cases.
- End each classic case with
breakto avoid fall-through. defaulthandles values that match no case.- Stacked cases can share one action on purpose.
- The modern arrow switch avoids break and can return a value.
Next, compare with if-else, or build a menu in the switch calculator.
Frequently Asked Questions
What is switch-case in Java?
Why do I need break in a switch?
break, Java keeps running the following cases too — this is called fall-through. Adding break at the end of each case stops it after the matching one, which is usually what you want.What is the default case?
default case runs when no other case matches. It works like the final else in an if-else ladder, handling any value you did not list.What types can a switch work on?
What is the modern switch in Java?
case 1 -> ...; which has no fall-through and needs no break. It can also return a value, making switch shorter and safer for many cases.