The Java switch statement uses the String parameter

Source: Internet
Author: User

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.

SolveGeneral 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.