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
1234567891011121314151617181920212223242526package 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
breakneeded; - 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 iffor comparisons and ranges; - Use
switchwhen checking one value against many exact options;
Thanks for your feedback!
Ask AI
Ask AI
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
1234567891011121314151617181920212223242526package 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
breakneeded; - 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 iffor comparisons and ranges; - Use
switchwhen checking one value against many exact options;
Thanks for your feedback!