Synchronize the original text to http://www.waylau.com/java-switch-use-string/
When I try to use the string parameter in a switch statement (note ctrType
as a string)
switch (ctrType) { case "01" : exceptionType = "读FC参数数据"; break; case "03" : exceptionType = "读FC保存的当前表计数据"; break; default: exceptionType = "未知控制码:"+ctrType; }
The following error is indicated:
Cannot switch on a value of type String for source level below 1.7. only convertible int values or enum variables is permitted
It means that my JRE version is too low to support. Before Java 7, the switch can only support byte, short, char, int, or its corresponding wrapper class and Enum type. In Java 7, string support was also finally added.
Solve common projects
Installing JDK 1.7+, changing the configuration in the project introduces the JDK version of the dependent library.
Maven Project
Change the Pom.xml file, set the Maven-compiler-plugin plug-in target version to 1.7 +, for example
<plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> ... </plugins>
The Java switch statement uses the String parameter