JDK 8增强了循环编译,但不是7 [英] Enhanced for loop compiling fine for JDK 8 but not 7

查看:154
本文介绍了JDK 8增强了循环编译,但不是7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码片断,在重构之后,我检查了为什么生成服务器报告了一个损坏的构建,但在我的IDE中没问题:

 列表与LT;字符串>文本; 
...
for(String text:text){...}



<因此,在for-each中使用相同的名称作为String和List。

这当然不是很明智的做法,但是在重命名之前遵循我的注意事项之后,我发现上面的代码用JDK 8编译得很好,但是给出了下面的代码错误与JDK 7:

$ $ p $ 错误:for-each不适用于表达式
for(String text:text) {
$
required:array or java.lang.Iterable
found:String
1 error

我知道在JDK中对这个区域的几个部分进行了修改 - 但是有人能够告诉我为什么发生这种情况吗?




更新:
因为我对不同的行为有了一些评论,所以下面是一个完整的示例类: b

  import java.util.Arrays; 
import java.util.List;

public class Strange {

List< String> text = Arrays.asList(Max,Alex,Maria);

public static void main(String [] args){
new Strange()。doSomething(Alex);

$ b $ public void doSomething(String name){
for(String text:text){
System.out.println(text.equals(Alex ));



$ b $ / code $ / pre
$ b $ p这里是编译过程和输出(Windows 7 64bit):

  C:\copy> c:\Projects\java\\ \\ jdk1.7.0_79\bin\javac.exe Strange.java 
Strange.java:13:error:for-each不适用于表达式类型
for(String text:text){
$
required:array or java.lang.Iterable
found:String
1 error
$ b $ C:\copy> c:\Projects\ java \jdk1.8.0_60\bin\javac.exe Strange.java

C:\copy>






结论:
我很困惑,为什么我的IDE(使用8)在一个语句中没有两次抱怨同一个名字 - 但是现在很明显,这不是一个声明。如果JLS另有说明,我真的很奇怪为什么这个问题已经存在很久了。但无论如何,感谢我收到的见解和伟大的答案(这使我很难挑选最好的答案)。

解决方案

虽然推理,使用循环到传统 for 循环,由其他 答案是正确的,有一个明确的规格范围:


§6.3 。声明的范围



...


$ b 声明的局部变量的作用域> FormalParameter 部分
增强用于语句(
§14.14.2
是包含的语句


direct link

因此,变量的范围并不包含 Java 7相比
Java 6
虽然bot h(我试过Java 6 javac )表现出矛盾的行为。



所以编译器行为的这种变化是修复一个老bug ...


Consider the following code snippet, I stumpled upon after some refactoring, when checkin why the build server reported a broken build but it was fine in my IDE:

List<String> text;
...
for (String text : text) {...}

So, the same name is used for the String and the List within the for-each.

This is of course not very wise to do, but after following my nosiness before renaming it, I saw that the above code compiles fine with JDK 8, but gives the below error with JDK 7:

  error: for-each not applicable to expression type
        for (String text : text) {
                           ^
  required: array or java.lang.Iterable
  found:    String
1 error

I know that changes were made to several parts in this area within the JDK - but can someone enlighten me on why exactly this behaviour occurs?


Update: Since I got some comments about different behaviour, here's a full sample class:

import java.util.Arrays;
import java.util.List;

public class Strange {

    List<String> text = Arrays.asList("Max", "Alex", "Maria");

    public static void main(String[] args) {
        new Strange().doSomething("Alex");
    }

    public void doSomething(String name) {
        for (String text : text) {
            System.out.println(text.equals("Alex"));
        }
    }

}

And here's the compile process and output (Windows 7 64bit):

C:\copy>c:\Projects\java\jdk1.7.0_79\bin\javac.exe Strange.java
Strange.java:13: error: for-each not applicable to expression type
        for (String text : text) {
                           ^
  required: array or java.lang.Iterable
  found:    String
1 error

C:\copy>c:\Projects\java\jdk1.8.0_60\bin\javac.exe Strange.java

C:\copy>


Conclusion: I was so puzzled why my IDE (which uses 8) didn't complain about twice the same name in one statement - but now it is clear that it is not one statement. I really wonder why this point has so long been in place if the JLS states otherwise. But anyway, thanks for the insights I have received and the great answers (which made it hard for me to pick the best one).

解决方案

While the reasoning, using the specified translation from the enhanced for loop to the traditional for loop, used by other answers is correct, there is an explicit specification about the scopes:

§6.3. Scope of a Declaration

The scope of a local variable declared in the FormalParameter part of an enhanced for statement (§14.14.2) is the contained Statement.

(direct link)

Thus, the scope of the variable does not include the Expression of the enhanced for loop…

You can verify that this hasn’t changed, compared to Java 7 and Java 6, though both (I tried Java 6 javac) exhibit the contradicting behavior.

So this change in the compiler behavior is the fix of an old bug…

这篇关于JDK 8增强了循环编译,但不是7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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