Java Switch
Map a value to a clear set of cases.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Switch.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A switch is useful when one value selects among named alternatives. This example uses traditional case labels and break statements. Modern switch expressions use different syntax; learn one form clearly before mixing the two.
Explore the details
Java Switch Statements
When you have many specific values to check, a switch statement is cleaner and more readable than a long chain of if...else if.
Think of it like a TV remote: press button 1 → Channel 1, press 2 → Channel 2, press anything else → nothing happens. Java evaluates the expression once, then jumps directly to the matching case.
Switch Statement Syntax
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
switch | Keyword that starts a switch statement |
day | The variable being matched against cases |
) { | Opening brace — cases go inside |
case | Each case checks for a specific value |
1 | The exact value this case matches |
break | Exits the switch — prevents fall-through to the next case |
default | Runs if no case matches — like the final else |
} | Closing brace — end of switch block |
How It Works
- Java evaluates the
switchexpression once. - It compares the result against each
casevalue from top to bottom. - When a match is found, that block of code runs.
breakexits the switch — without it, Java continues into the next case (fall-through).defaultruns if no case matches — it is optional.
The break Keyword
Without break, Java keeps running into the next case block even after a match — this is called fall-through. This is rarely what you want.
Fall-through printed three lines instead of one because there were no break statements. Always add break; after each case unless you intentionally want fall-through.
The default Keyword
The default block runs when no case matches. It works like the final else in an if-else chain and is optional but recommended.
switch vs if-else: Use switch when you're comparing one variable against many specific, known values. Use if-else when your conditions involve ranges (e.g. score >= 90) or complex expressions.
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;
default: System.out.println("Weekend!");
}
// Output: Wednesday
// Switch expression (Java 14+)
String type = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
default -> "Weekend";
};
System.out.println(type); // WeekdayA complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int choice = 2;
switch (choice) {
case 1: System.out.println("Learn"); break;
case 2: System.out.println("Practice"); break;
default: System.out.println("Choose 1 or 2");
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Practice
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Remove the break after case 1 and run with choice = 1.
Reveal the answer and reasoning / விடை
Learn and Practice are both printed because execution falls through to the next case. Restore the break afterwards.
One mistake to avoid: In traditional switch statements, missing break can cause fall-through. It is not automatically a compiler error.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.