带有字符串的 Java 7 switch 语句不起作用

标签 java eclipse string switch-statement java-7

根据 The Java Tutorials , 在 Java SE 7之后,您可以在 switch 语句的表达式中使用 String 对象。

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

有什么想法吗?

最佳答案

虽然 JDT 团队确实实现了 Switch on String feature ,Eclipse 3.7.1之前不会支持Java 7:

参见 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.

关于带有字符串的 Java 7 switch 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231907/

相关文章:

使用 jcifs 时 java.net.UnknownHostException : . .__MSBROWSE__

java - eclipse 未找到 JRE

java - 说明资源路径位置类型缺少包括 : test. xsl

c - 我不了解C字符串

java - 使用 libreoffice 将 Doc 转换为 docx 文件时出错

java - 在客户端和服务器之间将字符串转换为 byte[] 并返回字符串

java - 获取另一个类中 JComboBox 的索引

java - 截图: RasterFormatException (y+ height) is outside Raster

java - 检查字符串是否包含给定字符

java - 将 int 变量的名称存储在 String 中