Clean • Professional
yieldJava introduced Switch Expressions (Java 14) to make the switch construct more concise, expressive, and less error-prone. Along with this, the yield keyword (Java 13 preview → Java 14 stable) allows returning values from multi-statement switch cases.
These improvements help developers write cleaner code by reducing boilerplate and avoiding common mistakes like missing break.
yield in Java?yield is used inside a switch expression block to return a value when a case contains multiple statements.
In simple words: When your
switchcase has more than one line of code, you useyieldto return the final result.
Basic Example of Switch Expression
int day = 2;
String result = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Invalid";
};
System.out.println(result);
break statements needed👉 This is the foundation of modern switch expressions, where yield becomes useful for more complex cases.
yield was introduced?In modern switch expressions:
> to return values directly👉 That’s where yield is used.
yieldWhen a switch case contains multiple statements, Java uses yield to return a final value from the block.
int day = 2;
String result = switch (day) {
case 1 -> "Monday";
case 2 -> {
String msg = "Tuesday";
System.out.println("Processing...");
yield msg;
}
default -> "Invalid";
};
System.out.println(result);
here,
{ }yield returns the final computed value (msg)
yield Workscase 2 -> {
// multiple statements
yield value;
}
Execution Flow
yield returns the final value👉 In simple words: yield is used to return a value from a multi-line switch case block.
This example shows how yield can be used in real scenarios where a switch case contains additional logic before returning a value.
int marks = 85;
String grade = switch (marks / 10) {
case 10, 9 -> "A";
case 8 -> {
System.out.println("Good Performance");
yield "B";
}
case 7 -> "C";
default -> "Fail";
};
System.out.println(grade);
here,
switch (marks / 10) simplifies grade calculation.10, 9) can be grouped in one case.yield is used when a case has extra logic before returning a value.yield and return| Feature | yield | return |
|---|---|---|
| Used in | Switch expressions | Methods |
| Purpose | Returns value from switch block | Exits method and returns value |
| Scope | Inside switch block only | Entire method |
| Behavior | Ends only the current case block | Ends complete method execution |

yieldModern switch expressions make code shorter, cleaner, and less error-prone compared to the traditional switch statement.
int day = 2;
String result;
switch (day) {
case 1:
result = "Monday";
break;
case 2:
result = "Tuesday";
break;
default:
result = "Invalid";
}
String result = switch (day) {
case 1 -> "Monday";
case 2 -> {
yield "Tuesday";
}
default -> "Invalid";
};
Benefits of New Switch
break statements👉 Overall, modern switch expressions with yield make Java code more expressive and maintainable.
yield?Use yield in switch expressions when:
yield?Avoid using yield when:
> directly)yieldA common mistake is forgetting to use yield inside a multi-line switch block.
case 2 -> {
String msg = "Tuesday";
// missing yield
}
Correct Way
case 2 -> {
String msg = "Tuesday";
yield msg;
}
👉 In simple terms: If you use { } in a switch case, you must use yield to return a value.
This example shows how yield is useful in real applications like role-based access control systems, where different roles return different permissions.
String role = "ADMIN";
String access = switch (role) {
case "ADMIN" -> {
System.out.println("Full access granted");
yield "ALL_PERMISSIONS";
}
case "USER" -> "LIMITED_ACCESS";
default -> "NO_ACCESS";
};
System.out.println(access);
here,
ADMIN role executes multiple statements before returning a value.yield returns the final computed result from the block.USER and default use simple single-line returns.
yield is an important part of modern Java switch expressions, helping developers write cleaner and more structured code.
It allows you to:
👉 Overall, combined with switch expressions, yield makes Java more modern, expressive, and developer-friendly.