Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use String parameters in Javas witch statements

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use String parameters in Javas witch statements". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use String parameters in Javas witch statements.

problem

When I try to use the String parameter in the switch statement (note that ctrType is a string)

Switch (ctrType) {case "01": exceptionType = "read FC parameter data"; break;case "03": exceptionType = "read current meter data saved by FC"; break;default:exceptionType = "unknown control code:" + ctrType;}

The following error is 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 my jre version is too low to support. It is reported that before Java 7, switch could only support byte, short, char, int or their corresponding wrapper classes and Enum types. In Java 7, String support has finally been added.

Solve

General project

Install JDK 1.7 libraries, and change the configuration in the project to introduce the JDK version dependent library.

Maven project

Change the pom.xml file to set the target version of the maven-compiler-plugin plug-in to 1.7, for example

... org.apache.maven.pluginsmaven-compiler-plugin3.21.71.7...

At this point, I believe you have a deeper understanding of "how to use String parameters in Javas witch statements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development