9/20/2023 0 Comments Java switch expressionWhile the last sentence skips over the possibility of using case null:, it seems reasonable and offers a view into the language designers' intentions. In the judgment of the designers of the Java programming language, this is a better outcome than silently skipping the entire switch statement or choosing to execute the statements (if any) after the default label (if any). If the switch expression is of a reference type, that is, String or a boxed primitive type or an enum type, then a run-time error will occur if the expression evaluates to null at run time. The prohibition against using null as a switch label prevents one from writing code that can never be executed. The author meant to express that we should compute a value of numLetters for each day.As damryfbfnetsi points out in the comments, JLS §14.11 has the following note: Throw new IllegalStateException("Wat: " + day) Įxpressing this as a statement is roundabout, repetitive, and error-prone. Many existing switch statements are essentially simulations of switch expressions, where each arm either assigns to a common target variable or returns a value: int numLetters For example, in the following code, the many break statements make it unnecessarily verbose, and this visual noise often masks hard to debug errors, where missing break statements would mean accidental fall through. Whilst this traditional control flow is often useful for writing low-level code (such as parsers for binary encodings), as switch is used in higher-level contexts, its error-prone nature starts to outweigh its flexibility. The current design of Java's switch statement follows closely languages such as C and C++, and supports fall through semantics by default. These include the default control flow behavior between switch labels (fall through), the default scoping in switch blocks (the whole block is treated as one scope), and the fact that switch works only as a statement, even though it is often more natural to express multi-way conditionals as expressions. MotivationĪs we prepare to enhance the Java programming language to support pattern matching (JEP 305), several irregularities of the existing switch statement - which have long been an irritation to users - become impediments. Feedback on JDK 13 suggested that switch expressions were ready to become final and permanent in JDK 14 with no further changes. JEP 354 was targeted to JDK 13 in June 2019 as a preview feature. JEP 354 proposed a new statement, yield, and restored the original meaning of break. In response to the feedback, JEP 354 was created as an evolution of JEP 325. Feedback on JDK 12 suggested that this use of break was confusing. One aspect of JEP 325 was the overloading of the break statement to return a result value from a switch expression. JEP 325 was targeted to JDK 12 in August 2018 as a preview feature. Switch expressions were proposed in December 2017 by JEP 325. This was a preview language feature in JDK 12 and JDK 13. These changes will simplify everyday coding, and prepare the way for the use of pattern matching in switch. > labels (with no fall through), with a further new statement for yielding a value from a switch expression. : labels (with fall through) or new case. Extend switch so it can be used as either a statement or an expression, and so that both forms can use either traditional case.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |