The Java switch statement USES four major details of note

  • 2020-04-01 01:25:25
  • OfStack

The format of the switch statement is as follows :(its function is to select a piece of code to execute)
 
switch( Integer selection factor ) { 
case  An integer value 1 :  statements ; break; 
case  An integer value 2 :  statements ; break; 
case  An integer value 3 :  statements ; break; 
case  An integer value 4 :  statements ; break; 
case  An integer value 5 :  statements ; break; 
... 
default: statements ; 
} 

But a few things to note:
1. The parameter type of case in switch can only be int, but byte, short and char can also be played because byte, short and shar can be automatically promoted (automatic type conversion) to int, so it is still int in the final analysis. It is explained here that there are 8 data types in Java: Byte, short, char, int, long, float, double, and a Boolean, which cannot be converted to any type of data, small types can be automatically converted to large data types, and large data types to small data types must be cast.
2. After case, it can be an expression.
3. Break is used to jump out of the entire switch statement, if not, the next branch is executed.
4. Good programmers take advantage of default.

Related articles: