Java Switch

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 14 / 50 · English

Java Switch

Map a value to a clear set of cases.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 / கோடின் விளக்கம்

CodeMeaning
switchKeyword that starts a switch statement
dayThe variable being matched against cases
) {Opening brace — cases go inside
case Each case checks for a specific value
1The exact value this case matches
breakExits the switch — prevents fall-through to the next case
defaultRuns if no case matches — like the final else
}Closing brace — end of switch block

How It Works

  1. Java evaluates the switch expression once.
  2. It compares the result against each case value from top to bottom.
  3. When a match is found, that block of code runs.
  4. break exits the switch — without it, Java continues into the next case (fall-through).
  5. default runs 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.

Java reference fragmentJAVA
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);  // Weekday
03 / Read and predict

A 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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.