Java 7切换语句,字符串不起作用 [英] Java 7 switch statement with strings not working

查看:120
本文介绍了Java 7切换语句,字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Java教程,在 Java SE 7 及更高版本,您可以在switch语句的表达式中使用String对象。

According to The Java Tutorials, in Java SE 7 and later, you can use a String object in the switch statement's expression.

String s = ...
switch(s){
    //do stuff
}

但这是真的吗?我安装了JRE并将其添加到Eclipse项目的构建路径,但是我收到以下编译时错误:

But is this true? I've installed the JRE and added it to the build path of my Eclipse project, but I'm getting the following compile-time error:


无法打开
String类型的值。只允许可转换的int值或
枚举常量

Cannot switch on a value of type String. Only convertible int values or enum constants are permitted

此外,我认为自从我被能够使用它的 java.nio.file.Files 类,以及 JLayer

Also, I think I've got it configured correctly since I was able to use its java.nio.file.Files class, as well as JLayer.

任何想法?

推荐答案

虽然JDT团队确实实施了启用字符串功能,Java 7的支持将不会在Eclipse 3.7.1之前:

While it is true that the JDT team has implemented the Switch on String feature, the support for Java 7 won't be before Eclipse 3.7.1:

请参阅 bug 288548


由于JSR-292(Invoke Dynamic)和JSR-334(项目硬币)
和由于Java 7的正式发布日期(2011年7月28日)在3.7
之后,我们不得不将Java 7的支持推迟到3.7.1。还没有决定
是否可以作为3.7.1下载或单独的
功能更新的一部分。

Due to late availability of JSR-292 (Invoke Dynamic) and JSR-334 (Project Coin) and due to the official release date (July 28, 2011) of Java 7 being after 3.7 ships we had to defer the Java 7 support to 3.7.1. It has not yet been decided whether this will be available as part of the 3.7.1 downloads or as separate feature update.

工作由于Java 7功能目前正在BETA_JAVA7
分支中进行,我们将为稳定的版本提供单独的更新,以便
提供对有兴趣的方面的Java 7功能的早期访问。 p>

The work for the Java 7 features is currently in progress in the 'BETA_JAVA7' branch and we will deliver separate updates for the stable builds in order to provide early access to the Java 7 features for interested parties.

这篇关于Java 7切换语句,字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆