yield: a block that returns a value

Changes in new versions / Java 14

int month = 2;

// *** in version 14: ***
int days = switch (month) {
    case 1, 3, 5, 7, 8, 10, 12 -> 31;
    case 4, 6, 9, 11 -> 30;
    default -> {                          // a branch needs more than one line
        int year = 2026;
        boolean leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        yield leap ? 29 : 28;             // yield gives the value of the block
    }
};
System.out.println(days);

// yield also works in the old form with a colon:
int quarter = switch (month) {
    case 1, 2, 3: yield 1;
    default: yield 0;
};
System.out.println(quarter);