![]() 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. Statements to executed when none of the cases is trueĭespite being optional, the break statement is mostly used with switch case in Java.Let's have a critical look at the source code. An optional default statement is written at the end. Any number of case statements can be added to the code Statements to executed in case of value 2 is true Statements to executed in case of value 1 is true From JDK7, it can also be used with enumerated (Enum) data types in Java, the String class and Wrapper classes.įollowing is the Syntax of switch case in Java: switch(variable or an integer expression) The expression can be a byte, short, char, and int primitive data types. It is a multi-way branch statement that provides paths to execute different parts of the code based on the value of the expression. The switch case is very commonly used in Java. A Switch case statement is one of the conditional statements commonly found in almost every other programming language. A code is mainly comprised of series of conditions and their possible outputs in either case. The conditions and logical statements are an integral part of any program. Switch expression must be a constant value:. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |