Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

switch expression

A switch that produces a value, so it can be stored or printed.

A switch used as an expression (Stable since Java 14): each case supplies a value after -> or with yield, and the switch must be exhaustive.

Examples

  • int days = switch (month) { case 2 -> 28; case 4, 6, 9, 11 -> 30; default -> 31; };

Read the full section →