Java Switch Statement — Java Technical Interview Questions & Answers

Java Switch Statement

Java Switch Statement

  • switch picks one case from many options using one value — day number, menu choice, grade letter. You write switch(value), then case labels.
  • The matching case runs until break (or the end of switch). If you forget break, the next case also runs. That is called fall-through. default is like else: no case matched.
  • switch is cleaner than a long else-if chain when the value is a fixed list. It works with int, String, enum, and more in modern Java.
  • Interviewers almost always ask about missing break. Say it out loud: without break, cases fall through.
  • Let's take this on the board with one tiny Main class — no extra files. Java Switch Statement — day is 2, so case 2 prints Tue. break leaves the switch — case 1 and default do not run. Remove break and Tue + Other would both print (fall-through). Start from main, go line by line, and stop at each print. That output is the proof for Switch Statement.
  switch(day)
    case 1 → Mon  + break
    case 2 → Tue  + break
    default → Other

Technical Interview Tip: Explain break and fall-through with a 3-case example.

Code Examples for Java Switch Statement

Example

// Java Switch
public class Main {
  public static void main(String[] args) {
    int day = 2;
    switch (day) {
      case 1:
        System.out.println("Mon");
        break;
      case 2:
        System.out.println("Tue");
        break;
      default:
        System.out.println("Other");
    }
  }
}

Java Switch Statement — day is 2, so case 2 prints Tue. break leaves the switch — case 1 and default do not run. Remove break and Tue + Other would both print (fall-through).

How to Explain Java Switch Statement to an Interviewer

When do you use switch?

One value, many fixed options Menu / day / grade

What does break do in switch?

Leaves the switch Stops later cases from running

What is fall-through?

No break Next case also runs

What is default?

Like else Runs when no case matches