对于一个挑战,一位代码高尔夫球手编写了以下代码:

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);
    }
  }
}

当在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 

当在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 

在Java 10中,这种编号完全不存在。这里发生了什么?这是Java 10中的bug吗?

评论跟进:

The issue appears when compiled with Java 9 or later (we found it in 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. 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. Didier L simplified the issue with this much smaller and more understandable code: 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: evaluated Result when compiled in Java 9 and 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". To enable string concatenation, at least one of the sides must have type String. Trying to reproduce this on other types or constructs failed.


这是始于JDK 9的javac中的一个错误(在字符串连接方面做了一些更改,我怀疑这是问题的一部分),已由javac团队在错误id JDK-8204322下确认。如果你看这行对应的字节码:

array[i++%size] += i + " ";

它是:

  21: aload_2
  22: iload_3
  23: iinc          3, 1
  26: iload_1
  27: irem
  28: aload_2
  29: iload_3
  30: iinc          3, 1
  33: iload_1
  34: irem
  35: aaload
  36: iload_3
  37: invokedynamic #5,  0 // makeConcatWithConstants:(Ljava/lang/String;I)Ljava/lang/String;
  42: aastore

其中最后一个aaload是数组的实际负载。然而,这部分

  21: aload_2             // load the array reference
  22: iload_3             // load 'i'
  23: iinc          3, 1  // increment 'i' (doesn't affect the loaded value)
  26: iload_1             // load 'size'
  27: irem                // compute the remainder

它大致对应于表达式数组[i++%size](减去实际的load和store),在那里出现了两次。这是不正确的,正如jls-15.26.2中的规范所说:

形式为E1 op= E2的复合赋值表达式等价于E1 = (T) ((E1) op (E2)),其中T是E1的类型,只是E1只计算一次。

因此,对于表达式数组[i++%size] += i+ " ";,部分数组[i++%size]应该只计算一次。但是它被求值两次(一次为负载,一次为存储)。

是的,这是一个bug。


一些更新:

这个错误在JDK 11中被修复,并被反向移植到JDK 10(这里和这里),但没有移植到JDK 9,因为它不再接收公共更新。

Aleksey Shipilev在JBS页面上提到(并在评论中@DidierL):

解决方法:使用-XDstringConcat=inline编译

这将恢复到使用StringBuilder进行连接,并且没有错误。