Java Reference
In-Depth Information
For versions of Java prior to JDK 7, the expression controlling the switch must be of
type byte , short , int , char , or an enumeration. (Enumerations are described in Chapter
12 . ) Beginning with JDK 7, expression can also be of type String . This means that modern
versions of Java can use a string to control a switch . (This technique is demonstrated in
Chapter 5 , when String is described.) Frequently, the expression controlling a switch is
simply a variable rather than a larger expression.
Each value specified in the case statements must be a unique constant expression (such
as a literal value). Duplicate case values are not allowed. The type of each value must be
compatible with the type of expression .
The default statement sequence is executed if no case constant matches the expression.
The default is optional; if it is not present, no action takes place if all matches fail. When
a match is found, the statements associated with that case are executed until the break is
encountered or, in the case of default or the last case , until the end of the switch is reached.
The following program demonstrates the switch :
Search WWH ::




Custom Search