Switch-Expressions

1 post in this section

Switch Expressions (JEP 361): Switch as an Expression, Not Just a Statement

Finalized in Java 14 (JEP 361). Available in all Java 14+ releases, including Java 17. Previous previews: Java 12 (JEP 325) and Java 13 (JEP 354). The Problem with Switch Statements Java’s traditional switch statement has two well-known pain points: Pain 1: Fall-Through // Java 11 — easy to introduce fall-through bugs switch (day) { case MONDAY: result = "Start of work week"; break; // easy to forget this case TUESDAY: result = "Weekday"; // no break — falls through to WEDNESDAY case case WEDNESDAY: result = "Hump day"; break; default: result = "Other"; } A missing break causes the next case to execute unexpectedly.

Continue reading »