2011-06-03 32 views
10

根据The Java Tutorials,在Java SE 7及更高版本中,可以在switch语句的表达式中使用String对象。Java 7 switch语句不能使用字符串

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

但这是真的吗?我已经安装了JRE,并把它添加到我的Eclipse项目的构建路径,但我得到了下面的编译时错误:

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

另外,我想我已经得到了它,因为我正确配置能够使用它的java.nio.file.Files类,以及JLayer

任何想法?

+5

您是否有最近使用Java 7的Eclipse IDE?你正在使用哪个版本? Eclipse带来了自己的Java编译器! – Daniel 2011-06-03 19:45:31

+1

是的,试图在Eclipse之外编译它,看它是否工作。 – 2011-06-03 19:46:40

+0

@Daniel,我想我对IDE很幼稚。我认为使用适当的JRE配置构建路径会让事情变得顺利,看到特定于jdk7的功能正在使用Eclipse进行编译和运行。当这个功能没有编译/运行的时候,我想我只是被忽视了。我希望你不要被嘲讽... – mre 2011-06-03 20:08:42

回答

12

虽然这是事实,JDT团队已经实现了Switch on String feature,对Java 7的支持不会是Eclipse的3.7.1之前:

bug 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.

+0

任何发布日期为3.7.1?在同一天? – 2011-07-21 15:10:19

+0

@Bart:请参阅http://wiki.eclipse.org/Indigo/Simultaneous_Release_Plan#SR1“GA:9/23/2011(9月第四个星期五)” – VonC 2011-07-21 17:16:12

+2

2个月后? Ouch ......哦,我会用测试版。 – 2011-07-21 18:47:23

1

Eclipse不支持在JDK 7上进行编译,尝试使用NetBeans 7或使用标准javac编译器在JDK 7的包中手动或借助ant脚本编译所需的新功能。

0

使用Eclipse 3.8(第一个里程碑来自前几天)。不要忘记将编译器设置设置为Java 7.

+1

你是怎么做到的? – 2011-10-16 00:14:56

相关问题