Switch Statement Conditional Types

What type of information can follow a case in a switch conditional statement

What type of information can follow a case in a switch conditional statement

Prior to Java 7, switches could only support byte, short, char, int, or their corresponding wrapper classes, as well as Enum types. In Java 7, the String type is also supported.

Case must be followed by a constant

#define or const constants can be defined

Only the basic data types can be used with switch, these types include int, char, etc. This is a requirement for using switch

What is the difference between the data types that can be put in a switch statement and the other conditional statements? What is the difference between other conditional statements

java1.6 (including) before, just support for the basic type of data equivalent to int: byte, short, char, int (other are not allowed).

The new feature added in 1.7 supports String. long is not supported. Even by forced conversion, it must be converted to int.

The difference between switch and if-else:

switch is recommended for determining fixed values;

if is recommended for determining intervals or ranges;

What you can do with switch, you can do with if, but the other way around is not possible.

switch statement

Execution process:

First calculate and get the value of the expression or variable in the parentheses after the switch, and then compare the result of the calculation with the constants after each case in sequence.

When the two are equal, the code in the case block is executed, and when a break is encountered, the switch selection structure is skipped and the code after the switch selection structure is executed.

If the constant after any of the cases is equal to the value in the parentheses after the switch, the code in the default block at the end of the switch is executed.