eclipse 字符串不起作用的Java 7 switch语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6231907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 7 switch statement with strings not working
提问by mre
According to The Java Tutorials, in Java SE 7and later, you can use a String object in the switch statement's expression.
根据The Java Tutorials,在Java SE 7及更高版本中,您可以在 switch 语句的表达式中使用 String 对象。
String s = ...
switch(s){
//do stuff
}
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:
但这是真的吗?我已经安装了 JRE 并将其添加到我的 Eclipse 项目的构建路径中,但是我收到以下编译时错误:
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
无法打开字符串类型的值。只允许使用可转换的 int 值或枚举常量
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
.
另外,我想我已经正确配置了它,因为我能够使用它的java.nio.file.Files
类,以及JLayer
.
Any ideas?
有任何想法吗?
回答by VonC
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:
虽然 JDT 团队确实实现了Switch on String 特性,但对 Java 7 的支持不会早于 Eclipse 3.7.1:
See bug 288548:
请参阅错误 288548:
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.
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.
由于 JSR-292 (Invoke Dynamic) 和 JSR-334 (Project Coin) 的可用性较晚,并且由于 Java 7 的正式发布日期(2011 年 7 月 28 日)在 3.7 之后,我们不得不将 Java 7 支持推迟到 3.7 .1. 尚未决定这将作为 3.7.1 下载的一部分还是作为单独的功能更新提供。
Java 7 功能的工作目前正在“BETA_JAVA7”分支中进行,我们将为稳定版本提供单独的更新,以便为感兴趣的各方提供对 Java 7 功能的早期访问。
回答by aleroot
Eclipse doesn't support yet compilation on JDK 7, for try the new feature you need to use NetBeans 7 or compile with standard javac compiler in the bundle of the JDK 7 by hand or with the help of an ant script.
Eclipse 尚不支持在 JDK 7 上进行编译,要尝试使用 NetBeans 7 或使用 JDK 7 捆绑包中的标准 javac 编译器手动或借助 ant 脚本来尝试新功能。
回答by Hannes Licht
Use Eclipse 3.8 (first milestone came some days ago). Don't forget to set the compiler settings to Java 7.
使用 Eclipse 3.8(几天前第一个里程碑出现)。不要忘记将编译器设置设置为 Java 7。