Java Reference
In-Depth Information
Note the
default
case at the end. That's a
switch
block's way of letting you do something with input
that doesn't match any of the cases. In this example, we use it to catch the values between 18 and 24
(inclusive). We can do that because we know we won't get any other values. If we aren't sure that we
wouldn't get an unexpected value, we would probably throw an exception in the
default
case, to let
whatever code uses the
switch
block know that we found a problem.
One issue with a
switch
statement is that it cannot group its possible values (other than through the
default
keyword). Consequently, we end up with a lot of
case
labels that appear to do nothing. In fact,
each group of
case
statements shares the next block of code they encounter within the
switch
statement.
In this case, that means values 1 through 11 share the code that adds “morning” to the greeting and
values 12 through 17 share the code that adds “afternoon” to the greeting.
Another issue with
switch
statements is that each block of code generally requires a
break
statement. Each
break
statement makes the program jump from the break statement to the end of the
switch
statement. Consider what happens if we assume the input is 10. As written, the output is “Good
morning.” Without the
break
statements, the output is “Good morningafternoonevening.” That is, each
of the blocks is run. This happens because any
case
label without a
break
statement falls through to the
next
case
statement. That's the mechanism that lets multiple
case
labels share one block of code. With
no
break
statements, every block of code in the
switch
statement gets run. Although running more than
one block of code might be useful in some cases, it's generally an error (and can be hard to find because
the code probably looks right).
A final issue is the
default
keyword. A
switch
statement doesn't require a
default
section. However,
switch
statements often do include
default
sections. A
default
section performs the same function as a
final
else
statement in a group of
if
-
else
statements. As with the final
else
, you can use a
default
section for any remaining values, provided you are sure you won't get an unexpected value for the
switch
statement's argument. When you can't be sure of your input, use the
default
block to trap
unexpected values and take some appropriate action (often either doing nothing or throwing an
exception).
default
sections don't require
break
statements (the code “falls through” to the end of the
switch
statement anyway), but it's customary (and it's custom because it's a good idea) to add one, for
consistency and readability.
Let's consider another example in Listing 5-6 of a switch statement, this one being parallel in
function to Listing 5-4.
Listing 5-6. Using a switch statement without a default section
GregorianCalendar gregorianCalendar = new GregorianCalendar();
int day = gregorianCalendar.get(Calendar.DAY_OF_WEEK);
String greeting = "Good ";
switch (day) {
case 1: {
greeting += "Sunday";
}
case 2: {
greeting += "Monday";
}
case 3: {
greeting += "Tuesday";
}
case 4: {
greeting += "Wednesday";
}