Java Reference
In-Depth Information
case 6: foo();
case 5: foo();
case 4: foo();
case 3: foo();
case 2: foo();
case 1: foo();
} while (--q > 0);
}
Fortunately, this trick does not seem to be widely known or used. Moreover,
it is less needed nowadays; this sort of code transformation is properly in the
province of state-of-the-art optimizing compilers.
When the
switch
statement is executed, first the
Expression
is evaluated. If the
Expression
evaluates to
null
, a
NullPointerException
is thrown and the entire
switch
statement completes
abruptly for that reason. Otherwise, if the result is of a reference type, it is subject to un-
If evaluation of the
Expression
or the subsequent unboxing conversion (if any) completes
abruptly for some reason, the
switch
statement completes abruptly for the same reason.
Otherwise, execution continues by comparing the value of the
Expression
with each
case
constant, and there is a choice:
• If one of the
case
constants is equal to the value of the expression, then we say that
the
case
matches
, and all statements after the matching
case
label in the
switch
block,
if any, are executed in sequence.
If all these statements complete normally, or if there are no statements after the
matching
case
label, then the entire
switch
statement completes normally.
• If no
case
matches but there is a
default
label, then all statements after the matching
default
label in the
switch
block, if any, are executed in sequence.
If all these statements complete normally, or if there are no statements after the
de-
fault
label, then the entire
switch
statement completes normally.
• If no
case
matches and there is no
default
label, then no further action is taken and
the
switch
statement completes normally.
If any statement immediately contained by the
Block
body of the
switch
statement completes
abruptly, it is handled as follows:
• If execution of the
Statement
completes abruptly because of a
break
with no label,
no further action is taken and the
switch
statement completes normally.