Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn switch Statements in Java | Operators and Conditionals
Introduction to Java

switch Statements in Java

Swipe to show menu

So far, if, else, and else if have been the main tools for handling different situations. That works great for checking conditions, ranges, or comparisons.

But sometimes we're not comparing ranges at all — we just want to check one variable against several exact values. For example:

  • a customer chooses a payment method;
  • a coffee order has a specific pickup type;
  • a loyalty member selects a reward tier.

That's where switch statements become useful — they compare one value against multiple exact matches in a cleaner and more organized way.

Structure

switch (variable) {
    case "value1" -> {
        // code
    }

    case "value2" -> {
        // code
    }

    default -> {
        // fallback code
    }
}

Java checks each case one by one until it finds a match.

Example

Main.java

Main.java

1234567891011121314151617181920212223242526
package com.example; public class Main { public static void main(String[] args) { String pickupType = "delivery"; switch (pickupType) { case "pickup": System.out.println("Your coffee will be ready at the counter"); break; case "delivery": System.out.println("Your coffee is on the way"); break; case "dine-in": System.out.println("Enjoy your coffee inside the cafe"); break; default: System.out.println("Unknown order type"); break; } } }

Java checks pickupType case by case — "pickup" doesn't match, "delivery" does, so it prints "Your coffee is on the way" and the break immediately stops the switch. The default block at the bottom acts like a final else — it only runs when none of the cases match.

Java 14+ — New Switch Syntax

In Java 14 and later, a newer syntax was introduced using the -> arrow:

switch (pickupType) {
    case "pickup" -> System.out.println("Your coffee will be ready at the counter");
    case "delivery" -> System.out.println("Your coffee is on the way");
    case "dine-in" -> System.out.println("Enjoy your coffee inside the cafe");
    default -> System.out.println("Unknown order type");
}

What changed:

  • no break needed;
  • uses -> instead of :;
  • each case is automatically isolated — no fall-through risk;
  • cleaner and easier to read.

Both versions work the same — the new syntax is just more modern and safer.

A good rule to remember:

  • Use if / else if for comparisons and ranges;
  • Use switch when checking one value against many exact options;
question mark

What is the purpose of the break statement inside a switch?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

switch Statements in Java

So far, if, else, and else if have been the main tools for handling different situations. That works great for checking conditions, ranges, or comparisons.

But sometimes we're not comparing ranges at all — we just want to check one variable against several exact values. For example:

  • a customer chooses a payment method;
  • a coffee order has a specific pickup type;
  • a loyalty member selects a reward tier.

That's where switch statements become useful — they compare one value against multiple exact matches in a cleaner and more organized way.

Structure

switch (variable) {
    case "value1" -> {
        // code
    }

    case "value2" -> {
        // code
    }

    default -> {
        // fallback code
    }
}

Java checks each case one by one until it finds a match.

Example

Main.java

Main.java

1234567891011121314151617181920212223242526
package com.example; public class Main { public static void main(String[] args) { String pickupType = "delivery"; switch (pickupType) { case "pickup": System.out.println("Your coffee will be ready at the counter"); break; case "delivery": System.out.println("Your coffee is on the way"); break; case "dine-in": System.out.println("Enjoy your coffee inside the cafe"); break; default: System.out.println("Unknown order type"); break; } } }

Java checks pickupType case by case — "pickup" doesn't match, "delivery" does, so it prints "Your coffee is on the way" and the break immediately stops the switch. The default block at the bottom acts like a final else — it only runs when none of the cases match.

Java 14+ — New Switch Syntax

In Java 14 and later, a newer syntax was introduced using the -> arrow:

switch (pickupType) {
    case "pickup" -> System.out.println("Your coffee will be ready at the counter");
    case "delivery" -> System.out.println("Your coffee is on the way");
    case "dine-in" -> System.out.println("Enjoy your coffee inside the cafe");
    default -> System.out.println("Unknown order type");
}

What changed:

  • no break needed;
  • uses -> instead of :;
  • each case is automatically isolated — no fall-through risk;
  • cleaner and easier to read.

Both versions work the same — the new syntax is just more modern and safer.

A good rule to remember:

  • Use if / else if for comparisons and ranges;
  • Use switch when checking one value against many exact options;
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6
some-alt