The switch statement of jdk1.7 supports String data type

Before Java 7, switch could only support byte, short, char, int or their corresponding wrapper classes and Enum types. In Java 7, String support was added.

12345678910switch (ctrType) { case "01" : exceptionType = "Read FC parameter data"; break; case "03" : exceptionType = "Read the current meter data saved by FC"; break; default: exceptionType = "Unknown control code: "+ctrType; }

  where ctrType is a string.

  If used in versions before jdk 7, the following error will be prompted:

  Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

  It means that the jdk version is too low and not supported.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649289&siteId=291194637