In Java, this same pattern requires two changes: the condition must be in parentheses (), and the code block must be enclosed in curly braces {}. Listingย 4.1.2 shows how the simple if statement is written in Java.
Once again you can see that in Java the curly braces define a block rather than indentation. In Java, the parentheses around the condition are required because it is technically a function that evaluates to True or False.
Java does not have an elif pattern like Python. In Java you can get the functionality of an elif statement by nesting if and else. Here is a simple example in both Python and Java. Listingย 4.1.5 shows how it is written in Python.
We can get even closer to the elif statement by taking advantage of the Java rule that a single statement does not need to be enclosed in curly braces. Since the if is the only statement used in each else block, we can get away with Listingย 4.1.7.
Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions.Listingย 4.1.9 shows how a grade program using a switch statement would look in Python.
The match - case statement was introduced in Python 3.10, so doesnโt run in earlier version of Python. Here is an example using Pythonโs match - case structure.
The switch statement in Java provides an alternative to chaining multiple if-else conditions, when comparing a single variable against several constant values. It supports a variety of data types, including primitive types (byte, short, char, int), their wrapper classes, enumerations, and String (introduced in Java 7). Each case within a switch must be defined using a constant expression, and duplicate case values are not permitted. By default, control flow "falls through" from one case to the next unless a break, return, or throw statement is used to terminate execution.
Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. yield is used inside a switch expressionโs block to produce the value of that expression, unlike break which simply exits a switch statement or loop. Itโs important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs. Listingย 4.1.10 shows how the switch expression is written in Java.
The switch statement is not used very often, and we recommend you do not use it. First, it is not as powerful as the else if model because the switch variable can only be compared for equality with an integer or enumerated constant. Second, it is very easy to forget to put in the break statement, so it is more error-prone. If the break statement is left out then then the next alternative will be automatically executed. For example, if the grade was 95 and the break was omitted from the case 9: alternative then the program would print(out both A and B.)
Finally, the switch statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the elif. Even with the new features of Java 14+ the switch statement is still limited to constant comparisons using equality.