← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 7 of 12

Java Switch Expressions

A switch expression is a switch that gives back a value, like a vending machine that hands you a result for each button. It is shorter and safer than the old style.
Syntax
java
result = switch (expression) {
    case value1 -> result1;
    case value2, value3 -> result2;
    default -> defaultResult;
};

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

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare day and set it to 3
		int day = 3;
		String name = switch (day) {
			case 1 -> "Monday";
			case 2 -> "Tuesday";
			case 3 -> "Wednesday";
			default -> "Unknown";
		};
		// Print a line to the console
		System.out.println(name);
	}
}

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

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare score and set it to 85
		int score = 85;
		String grade = switch (score / 10) {
			case 10, 9 -> "A";
			case 8 -> {
				// Declare g and set it to "B"
				String g = "B";
				yield g;
			}
			default -> "C";
		};
		// Print a line to the console
		System.out.println(grade);
	}
}

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

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare day and set it to 6
		int day = 6;
		String type = switch (day) {
			case 1, 2, 3, 4, 5 -> "Weekday";
			case 6, 7 -> "Weekend";
			default -> "Invalid";
		};
		// Print a line to the console
		System.out.println(type);
	}
}

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

java
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);
	}
}

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

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Leaving out default when switching on an int or String, which is a compile error because a switch expression must cover every possible value.
  2. Using return inside a block of a switch expression case, when the value must be produced with yield.
  3. Mixing arrow cases (->) and colon cases (:) in the same switch, which is not allowed.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.