Switch expressions and yield
What you will learn
A switch expression produces a value rather than merely selecting an action. This helps when one choice determines a fee, label or configuration value. The compiler checks that the switch covers all required selector possibilities and that its result paths are valid.
public class Main { public static void main(String[] args) { String size = "medium"; int price = switch (size) { case "small" -> 4; case "medium" -> 6; case "large" -> 8; default -> 0; }; System.out.println(price); }}Expected output:
6The matched expression supplies 6. Here zero is a defined unknown-size result, not a valid price.
How it works
Write int value = switch (selector) { case 1 -> 10; default -> 0; };. The final semicolon finishes the declaration. For ordinary int or String constant cases, add default for unmatched values. Some exhaustive enum and pattern switches need no default, studied later. A simple expression supplies its value directly. A rule block can calculate a result; use yield on each path that would otherwise complete normally. A rule can instead throw an exception, which aborts evaluation. Yield supplies the enclosing switch expression’s value, break exits a statement switch or loop, and return exits a method. Null selectors need a guard or explicit case null. Switch expressions and yield are permanent starting in Java 14; case null is standard starting in Java 21.
public class Main { public static void main(String[] args) { String service = "express"; int fee = switch (service) { case "pickup" -> 0; case "express" -> { int base = 5; int surcharge = 3; yield base + surcharge; } default -> 4; }; System.out.println(fee); }}Expected output:
8The block calculates a sum and yields it to the outer assignment. Other service codes use the standard fee under this example contract.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { String status = null; String label = switch (status) { case null -> "Missing"; case "ready" -> "Ready"; default -> "Other"; }; System.out.println(label); }}Expected output:
MissingThe explicit null case matches a missing reference. Java 14–20 learners can guard null before switching.
Common mistakes
A rule block that only prints a value does not supply a result. Add yield when the rule is meant to complete successfully. Return cannot jump out of an enclosing switch expression. An ordinary int switch expression with only case 1 is not exhaustive.
Try it yourself
Calculate service duration: code 1 takes 10 minutes, code 2 takes base 15 plus 5, other codes produce -1 (unknown). Use yield in the code 2 block. Test 2, 1 and 0 independently.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Switch expressions and yield — knowledge check
Apply switch expressions and yield to examples and boundary conditions.
What does yield do in a switch expression block?
An int switch expression contains only case 1 -> 5;. What is wrong?
What is missing from int n = switch (code) { default -> 0; }?
Which Java version made switch expressions permanent?
Which rule explicitly handles a null String selector in Java 21?