Java Reference
In-Depth Information
As you would expect, the output from the program is
The string contained in
command
(which is "cancel" in this program) is tested against the
case
constants. When a match is found (as it is in the second
case
), the code sequence as-
sociated with that sequence is executed.
Being able to use strings in a
switch
statement can be very convenient and can improve
the readability of some code. For example, using a string-based
switch
is an improvement
over using the equivalent sequence of
if
/
else
statements. However, switching on strings can
be less efficient than switching on integers. Therefore, it is best to switch on strings only