新版本的变更 / Java 14

int month = 2;

// *** in version 14: ***
int days = switch (month) {
    case 135781012 -> 31;
    case 46911 -> 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 123yield 1;
    defaultyield 0;
};
System.out.println(quarter);