为什么数组[idx ++] + =“a”在Java 8中增加一次idx,在Java 9和10中增加两次?

烂漫一生 提交于 2020-04-28 22:36:29

问题:

For a challenge, a fellow code golfer wrote the following code : 对于挑战, 一位代码高尔夫球手 编写了以下代码

import java.util.*;
public class Main {
  public static void main(String[] args) {
    int size = 3;
    String[] array = new String[size];
    Arrays.fill(array, "");
    for(int i = 0; i <= 100; ) {
      array[i++%size] += i + " ";
    }
    for(String element: array) {
      System.out.println(element);
    }
  }
}

When running this code in Java 8, we get the following result: 在Java 8中运行此代码时,我们得到以下结果:

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100 
2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89 92 95 98 101 
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 

When running this code in Java 10, we get the following result: 在Java 10中运行此代码时,我们得到以下结果:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 

The numbering is entirely off using Java 10. So what is happening here? 编号完全取决于使用Java 10.那么这里发生了什么? Is it a bug in Java 10? 这是Java 10中的错误吗?

Follow ups from the comments: 从评论中跟进:

  • The issue appears when compiled with Java 9 or later (we found it in Java 10). 使用Java 9或更高版本编译时会出现此问题(我们在Java 10中找到它)。 Compiling this code on Java 8, then running in Java 9 or any later version, including Java 11 early access, gives the expected result. 在Java 8上编译此代码,然后在Java 9或任何更高版本(包括Java 11早期访问)中运行,可以得到预期的结果。
  • This kind of code is non-standard, but is valid according to the spec. 这种代码是非标准的,但根据规范有效。 It was found by Kevin Cruijssen in a discussion in a golfing challenge , hence the weird use case encountered. 凯文·克鲁伊森Kevin Cruijssen)高尔夫挑战赛讨论中发现了这一点,因此遇到了奇怪的用例。
  • Didier L found out that the issue can be reproduced with the much smaller and more understandable code: Didier L发现可以使用更小,更易理解的代码重现该问题:

    class Main { public static void main(String[] args) { String[] array = { "" }; array[test()] += "a"; } static int test() { System.out.println("evaluated"); return 0; } }

    Result when compiled in Java 8: 在Java 8中编译时的结果:

    evaluated

    Result when compiled in Java 9 and 10: 在Java 9和10中编译时的结果:

    evaluated evaluated
  • The issue seems to be limited to the string concatenation and assignment operator ( += ) with an expression with side effect(s) as the left operand, like in array[test()]+="a" , array[ix++]+="a" , test()[index]+="a" , or test().field+="a" . 问题似乎仅限于字符串连接和赋值运算符( += ),其中带有副作用的表达式作为左操作数,如array[test()]+="a"array[ix++]+="a"test()[index]+="a" ,或test().field+="a" To enable string concatenation, at least one of the sides must have type String . 要启用字符串连接,至少有一个边必须具有String类型。 Trying to reproduce this on other types or constructs failed. 尝试在其他类型或构造上重现此操作失败。


解决方案:

参考一: https://stackoom.com/question/3QfAQ/为什么数组-idx-a-在Java-中增加一次idx-在Java-和-中增加两次
参考二: https://oldbug.net/q/3QfAQ/Why-does-array-idx-a-increase-idx-once-in-Java-8-but-twice-in-Java-9-and-10
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!