1.7 Switch Statement Java


22/05/25

4 mins

Java Switch Statement: How and When to Use It

If you want your Java code to easily choose one out of many possible execution paths, the switch statement is your friend. It simplifies complex branching logic that would otherwise require a long series of if-else statements. Let’s see how it works, best practices, and some practical examples—all using clear, beginner-friendly language and real Java code.

What is a Switch Statement?

A switch statement lets you check a value and then run code based on its exact match. Instead of stacking up many if-else statements, you match a variable to case labels inside a single block. Each case has related code, and there’s usually a default for unmatched values.

How a Switch Statement Works

  • Selector variable: The value you check (must be one of the supported types listed below)
  • Case labels: Constant values to match against your variable
  • Break statements: End a case so code doesn’t accidentally run the next one (prevents “fall through”)
  • Default label: Runs if no cases match

Supported types for switch selector

You can use:

  • byte, short, char, int
  • Wrapper types: Byte, Short, Character, Integer
  • String (Java 7 and later)
  • Enum types

You cannot use: boolean, long, float, or double for the selector variable.

Basic Example

Here’s a simple switch statement using an int:

int quarter = ...; // any value String quarterLabel = null; switch (quarter) { case 0: quarterLabel = "Q1 - Winter"; break; case 1: quarterLabel = "Q2 - Spring"; break; case 2: quarterLabel = "Q3 - Summer"; break; case 3: quarterLabel = "Q3 - Summer"; break; default: quarterLabel = "Unknown quarter"; }
  • After checking quarter, the code jumps into the matching case.
  • The break keyword exits the switch; without it, “fall through” happens (more below).

Fall-Through and Multiple Cases

If you leave out break, Java continues running all remaining code until it finds a break or the end of the switch. Sometimes, you want this:

List futureMonths = new ArrayList<>(); int month = 8; switch (month) { case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); break; default: break; }

If month is 8, this fills futureMonths with "August" through "December".

Multiple Labels per Case

You can stack multiple case labels for shared logic (great for months with the same number of days):

int month = 2, year = 2021, numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ((year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; // leap year else numDays = 28; break; default: System.out.println("Invalid month."); break; }

Here, case 1, case 3, case 5, etc., all lead to the same code.

Using Strings in a Switch

Since Java 7, you can use String expressions in a switch statement:

String month = ...; // any month int monthNumber = -1; switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7; break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break; case "december": monthNumber = 12; break; default: monthNumber = 0; break; }

Make sure to handle upper/lowercase by converting with .toLowerCase().

Comparing switch with if-then-else

  • switch: Use when checking a single variable against many known constant values for clarity and speed.
  • if-then-else: Better for testing ranges, more complex logic, or boolean conditions.

For example, you cannot use a switch for:

if (temperature < 0) { System.out.println("Water is ice"); } else if (temperature < 100){ System.out.println("Water is liquid"); } else { System.out.println("Water is vapor"); }

Since switch can’t handle ranges or booleans directly.

Don't Forget: Default and Null Values

  • The default label catches anything not covered by your cases.
  • If your selector variable is an object (like String), make sure it’s not null—otherwise, the switch will throw a NullPointerException.

Quick Tips

  • Always use break by default to prevent fall-through unless you purposely want it.
  • Group case labels when they share the same logic.
  • Use default to catch unexpected values.
  • Use String with switch statements for readable code (Java 7+).

The switch statement makes your decision-making code cleaner, easier to understand, and lets you handle many cases without long if-else ladders. Try using it in your own Java programs for menus, user options, and more!

Happy Coding!


PREVIOUS: 1.6 Expressions, Statements, and Blocks in Java

NEXT: 1.8 Java Switch Expressions: Modern Branching Made Easy