Article Directory
Bug && fix
In switch case
the code block, I want to enum
make a case distinction for the type of object. But an error is reported:
that is to say, case
the label behind needs to be changed, and it cannot be written like this ( enumeration type name. enumeration value ).
How to write it? Remove the enumeration type name. Just write the enumeration value .
Even if it is an enumeration class defined by yourself, the usage is the same.
Java switch official documentation
First of all, go to the official website document: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
The following part of the document will be translated and analyzed.
- What are the variable types supported by the switch code block?
The switch Statement
A switch works with the byte, short, char, and int primitive data types.
It also works with enumerated types (discussed in Enum Types), the String class,
and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).
Basic type: byte short char int
String String
Enumeration type enum
Special type (a class that encapsulates the four supported basic types): Byte Short Character Integer
.
That is to say, there are 4+2+4 or 10 types in total.
- The String type is supported from JDK 1.7 (switch code block).
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement's expression.