Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 192
Watch Semicolons in switch Expressions
ОглавлениеUnlike a regular switch
statement, a switch
expression can be used with the assignment operator and requires a semicolon when doing so. Furthermore, semicolons are required for case
expressions but cannot be used with case
blocks.
var name = switch(fish) { case 1 -> "Goldfish" // DOES NOT COMPILE (missing semicolon) case 2 -> {yield "Trout";}; // DOES NOT COMPILE (extra semicolon) … } // DOES NOT COMPILE (missing semicolon)
A bit confusing, right? It's just one of those things you have to train yourself to spot on the exam.