Skip to content

YieldStmt Statement

YieldStmt Entity

In Java, the yield keyword is used within switch expressions to return a value from a case label. It was introduced in Java 13 as part of switch expressions to provide a way to return values from individual cases. Unlike the traditional break statement, yield allows you to produce a value that becomes the result of the entire switch expression. This is particularly useful when you need to compute and return different values based on different cases in a more concise and expressive way than traditional switch statements.

Example

String message = switch (number) {
case ONE -> {
yield "Got a 1";
}
case TWO -> {
yield "Got a 2";
}
default -> {
yield "More than 2";
}
};

Attributes

MethodDescription
GetAPrimaryQlClass() stringReturns the primary CodeQL class name for this entity
GetHalsteadID() intReturns the Halstead ID metric for this statement
GetPP() stringReturns the pretty-printed representation of this statement
ToString() stringReturns a string representation of this statement
GetValue() *ExpReturns the expression being yielded in this statement