Java 14: Switch expression with yield and a block

Use a block body and yield to compute a switch branch value.

Code
int score = 82;
String grade = switch (score / 10) {
    case 10, 9 -> "A";
    case 8 -> { yield score >= 85 ? "B+" : "B"; }
    case 7 -> "C";
    default -> "F";
};
System.out.println(grade);
Output
B
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26