Java Reference
In-Depth Information
check.isValidSwitchType(varTypeClass(new
Integer(7)));
check.isValidSwitchType(varTypeClass("Java
8 Rocks!"));
check.isValidSwitchType(varTypeClass(new
Long(7)));
check.isValidSwitchType(varTypeClass(true));
check.isValidSwitchType(varTypeClass(java.nio.file.AccessMode.READ));
}
}
Here is the result of executing SwitchTypeChecker :
Primitive type char is a valid switch type.
Primitive type int is a valid switch type.
double is not a valid switch type.
Primitive type short is a valid switch type.
Boxed primitive type Integer is a valid switch type.
String is a valid switch type.
Long is not a valid switch type.
boolean is not a valid switch type.
Enum is a valid switch type.
How It Works
The switch statement is a control-flow statement that allows you to execute different
blocks of code based on the value of a switch expression. It is similar to the if-
then-else statement, except that the switch statement can have only a single test
expression, and the expression type is restricted to one of several different types. When
a switch statement executes, it evaluates the expression against constants contained
in the switch statement's case labels. These case labels are branch points in the
code. If the value of the expression equals the value of a case label constant, control
is transferred to the section of code that corresponds to the matching case label. All
code statements from that point on are then executed until either the end of the
switch statement is reached or a break statement is reached. The break statement
causes the switch statement to terminate, with control being transferred to the state-
ment following the switch statement. Optionally, the switch statement can contain
Search WWH ::




Custom Search