Java Switch Expressions
In this page:
Classic Switch vs Switch Expressions
Switch expressions let you return a value directly from each case using the arrow (->) operator, evaluating to a result you can assign or return. This is cleaner than classic switch statements because it removes case fall-through entirely, along with the need for explicit break statements at the end of every case.
Example: Classic Switch vs Switch Expressions
public class Main {
public static void main(String[] args) {
int day = 3;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Unknown";
};
System.out.println(name);
}
}
Login to try C/C++/Java/PHP code in the editor
The yield Statement
If a case's body needs more than a single expression, wrap that code in curly braces and use the yield statement to specify the value that block ultimately produces. Yield plays the same role inside a switch expression's block that return plays inside a method.
Example: The yield Statement
public class Main {
public static void main(String[] args) {
int score = 85;
String grade = switch (score / 10) {
case 10, 9 -> "A";
case 8 -> {
String g = "B";
yield g;
}
default -> "C";
};
System.out.println(grade);
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Case Labels
You can group several case labels together on one line by separating the values with commas, so multiple inputs share the exact same arrow-case body instead of duplicating that body for each value individually.
Example: Multiple Case Labels
public class Main {
public static void main(String[] args) {
int day = 6;
String type = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Invalid";
};
System.out.println(type);
}
}
Login to try C/C++/Java/PHP code in the editor
Exhaustiveness and default
A switch expression must be exhaustive, meaning it has to account for every possible value the switched-on expression could take. If the compiler can't already prove that from the type itself (as it can with sealed types or enums), you must supply a default case to cover anything unmatched.
Example: Exhaustiveness and default
public class Main {
enum Level { LOW, MEDIUM, HIGH }
public static void main(String[] args) {
Level level = Level.MEDIUM;
String msg = switch (level) { // compiler proves exhaustiveness from the enum, no default needed
case LOW -> "low";
case MEDIUM -> "medium";
case HIGH -> "high";
};
System.out.println(msg);
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Evaluation
Arrow case labels aren't limited to constant literals — they can invoke external methods to compute and return a value dynamically, letting a switch expression's cases run arbitrary logic rather than just picking between fixed constants.
Example: Dynamic Evaluation
public class Main {
static int square(int n) { return n * n; }
public static void main(String[] args) {
int code = 2;
int result = switch (code) {
case 1 -> 10;
case 2 -> square(5); // arbitrary method call, not just a constant
default -> 0;
};
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: