The switch statement differs from the If-else statement in that the switch statement can have multiple possible execution paths. In the fourth edition of Java programming idea, the syntax format of the switch statement is described:
Switch (integral-selector) { case integral-value1: statement; Break ; Case integral-value12: statement; Break ; default : statement; }
where Integral-selector (integer selection factor) is an expression that can produce an integer value. It also indicates that the selection factor must be an integer value of int or char, or an enum enum type. Since the fourth edition of Java programming thought was written on the basis of JDK1.5, it is inevitable that there will be some obsolescence in the present book.
In the description of the switch statement found in the Java official manual, a switch works with the byte, short, char, and int primitive data types has been explicitly identified. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive Types:chara Cter, Byte, short, and Integer. where enum enum types are new additions in JDK1.5, and switch supports string strings after JDK7.
When programming using the JDK7 version of the environment, the selection factor of the switch statement is used, and if the specification is not met (such as the float type variable), a hint is generated: cannot switch on a value of type float . Only convertible int values, strings or enum variables is permitted because, Byte,short,char can be implicitly converted to I NT so the INT type variable also contains the Byte,short,char type variable.
Summarize:
the data types that can be used as switch selection factors are:
- Char,byte,short,int and their packaging class Character,byte,short,integer
- ENMU enumeration (since jdk1.5)
- String string (since jdk1.7)
Java Programming Ideas (fourth Edition) Learning notes----4.8 switch (knowledge points updated)