The yield keyword in Java

Introduced in Java 13 as part of the enhancements in Project Amber, the ‘yield‘ keyword aims to simplify code, making switch expressions more concise and expressive. Let us learn about ‘yield‘ keyword, its purpose, syntax, and see some practical examples.

1. The ‘yield‘ Keyword

The ‘yield’ keyword enhances the switch expressions by allowing the expression to produce a result directly. Let us understand with an example.

In the following program, the switch expression evaluates the variable ‘day‘ value.

  • If ‘day’ matches any of the weekday (MON through FRI), it prints “It is WeekDay” to the console and sets the boolean result to true.
  • If ‘day’ matches SAT or SUN, it prints “It is Weekend” to the console and sets the boolean result to false.
  • The true and false values are implicitly returned as results for the corresponding cases.
Boolean result = switch(day) {

    case MON, TUE, WED, THUR, FRI -> {
        System.out.println("It is WeekDay");
        true;
    }
    case SAT, SUN -> {
        System.out.println("It is Weekend");
        false;
    }
};

System.out.println("Result is " + result);

Let us rewrite the above program using the yield keyword again, and notice the difference.

Boolean result = switch(day) {

    case MON, TUE, WED, THUR, FRI -> {
        System.out.println("It is WeekDay");
        yield true;
    }
    case SAT, SUN -> {
        System.out.println("It is Weekend");
        yield false;
    }
};

System.out.println("Result is " + result);

In this version, the only difference is the explicit use of the ‘yield‘ keyword which serves two purposes:

  • It provides a value for the switch expression.
  • It acts as a terminator, indicating that the control flow should exit the switch expression.

You can notice that both versions of the program achieve the same functionality. However, the ‘yield‘ keyword in the second version adds explicitness to the return statement within the case blocks, thus making it clear that a value is being returned and terminating the switch expression.

2. Difference between ‘yield’ and ‘return’ Keywords

The ‘yield’ and ‘return’ keywords in Java serve distinct purposes and are used in different contexts.

  • A return statement returns control to the invoker of a method or constructor.
  • A yield statement transfers control by causing an enclosing switch expression to produce a specified value.

Let us list down the differences in more detail:

FeatureYieldReturn
Used InMethods, constructors, loops, lambda expressionsSpecifically within switch expressions
TerminationTerminates the execution of the entire method.Terminates the execution of the switch expression
ScopeGeneral-purpose keyword used in various contexts.Specialized for use within switch expressions and is not applicable elsewhere.

Drop me your questions in the comments related to the enhanced switch statement with yield keyword.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.