Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Why can't SWITCH in JAVA use LONG as a parameter?
Why can't SWITCH in JAVA use LONG as a parameter?
The fundamental reason why switches in JAVA can't use long is that converting to int for a long time will lose precision and lead to inaccurate data, so there is a logical rule that JAVA switches are not allowed to use long.

The variable type in the switch statement can be: byte, short, int or char. Since Java SE 7, switches support string types, and case labels must be string constants or literals.

Extended data

In java, Switch case has the following rules:

A switch statement can have multiple case statements. Each case is followed by a value and a colon for comparison.

The data type of the value in the case statement must be the same as that of the variable, and it can only be a constant or a literal constant.

When the value of the variable is equal to the value of the case statement, the statement after the case statement begins to execute until the break statement jumps out of the switch statement.

When the break statement is encountered, the switch statement terminates. The program jumps to the statement execution after the switch statement. A case statement does not necessarily contain a break statement. If the break statement does not appear, the program will continue to execute the next case statement until the break statement appears.

A switch statement can contain a default branch, which is usually the last branch of the switch statement (it can be anywhere, but the last branch is recommended). When the case statement has no value and the values of variables are equal, the default value will be executed. The default branch does not require a break statement.

References:

Baidu encyclopedia -JAVA