Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 11

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.

💡 Use switch when one variable is compared to many exact values (like a menu choice or a day number).

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");
}
Wednesday

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");
}
One
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");
}
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;
}
Spring

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);
Wednesday

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 > 5 is 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 break to avoid fall-through.
  • default handles 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?
A switch-case checks one value against several fixed options (cases) and runs the matching one. It is a cleaner alternative to a long else-if ladder when you compare the same variable to many exact values.
Why do I need break in a switch?
Without 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?
The 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?
A traditional switch works on int, char, String, and enum values (and their wrapper types). It compares the variable to constant case values, so each case must be a fixed literal.
What is the modern switch in Java?
Newer Java versions add an arrow form: 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.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicLoops (for, while, do-while) Your progress: 11 / 61 (18%) · Lesson 12 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free