7/31/2023 0 Comments Java switch exampleThis blog introduced the benefits and simplifications of the new syntax of switch so that you can profit from it in your own projects. Let me conclude: The new syntax of switch seems to be just a small change, but it has an enormous effect on readability and ease of use. These are covered in one of the next blogs. Another is the multi-line strings, called Text Blocks. Fortunately, Java 14 brings a good slew of useful enhancements: On the one hand, there are the convenient syntax changes in switch. This is true even for Java 11 as an LTS version. The Java releases up to and including 13 are rather manageable in terms of their innovations. Furthermore, if there were a case JULY, it would always be executed, regardless of the position of the default, thus analogous to the behavior of the previous switch statement. Unlike the old syntax, the input Month.JULY just no longer results in an unexpected output "February", but as specified by default in "N/A". Thus the input Month.FEBRUARY returns "February" as expected, and moreover the default in the middle of the cases is not quite as dramatic, though certainly not pretty either. Furthermore, the fall-through does not occur. It is especially worth mentioning that one can directly return the value calculated by the switch construct. Static String monthToName(final Month month)ĭefault -> "N/A" // NO fall-through here While it is still quite clear for enums with a limited set of possible values, the following question arises: How does it work for other types, such as ints? In such cases, the compiler can only state that possibly not all values are covered, and complains: "A switch expression should cover all possible values." Then the IDEs suggest to add a default – here indicated by the line commented out: This innovation is necessary because the switch must now return a value in each case. Then this is directly criticized by the compiler: "A switch expression should cover all possible values." Additionally, IDEs offer the choice of adding either the appropriate case or a default, but not both: full coverage of the enum values is automatically detected. The handling has conveniently improved with the new switch expressions: Let’s assume we did not specify WEDNESDAY in the above example. This would only have been pointed out later when accessing the variable used in the example that it is not initialized in every case. The compiler wouldn't have recognized this in switch itself. In the old version of switch it was possible to omit the default or to specify a case for individual values, for example WEDNESDAY. Furthermore, this is no longer allowed in the new syntax after the arrow. And even better, of course, still without the need for a break. Instead of just stating a value after the arrow, it is also possible to specify expressions such as assignments or method calls without any problems. Besides, the switch can now return a value, which avoids the need to define auxiliary variables. Conveniently, there is no more need for break: The statements after the arrow are only executed specifically for the case and no fall-through exists with this syntax. With the new "Switch Expressions", expressing case distinctions is made much easier and provides an intuitive notation:įrom this example, we notice some syntactic innovations: In addition to the obvious arrow instead of the colon, multiple values can now be specified after the case. Otherwise, the compiler complains that the variable numOfLetters may not be initialized – unless you have already assigned a value to it initially. ![]() ![]() In particular, despite the actually complete coverage of the enum values, the default is necessary. Moreover, we need to set the (artificial) auxiliary variable numOfLetters correctly in each branch. Even worse, a break is needed so that the processing runs without surprise and there is no fall-through. ![]() The multiple specifications of values need accustoming, too. First of all, the shown construct does not appear elegant and is also quite long. For the last two cases, I wrote the code a bit more complicated than necessary to demonstrate what is possible with switch expressions.Let's have a critical look at the source code. In the following example, we print the word length for a given day of the week. Let's look at the changes one by one (I'm using the examples from JEP 361, slightly modified). Using switch as an expression with a return value.Arrow notation without break and fall-throughs The switch statement is pretty primitive as control structures go, and its easy to make mistakes when you use it.Switch Expressions are actually two enhancements that can be used independently but also combined: Switch Expressions were released in Java 14 under Project Amber.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |