首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么阵列[idx++]+=“a”在Java8中增加了一次idx,而在Java9和10中增加了两次?

为什么阵列[idx++]+=“a”在Java8中增加了一次idx,而在Java9和10中增加了两次?
EN

Stack Overflow用户
提问于 2018-06-04 23:16:14
回答 1查看 73.3K关注 0票数 776

挑战,a fellow code golfer wrote the following code

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 

使用Java10完全取消了编号。那么这里发生了什么呢?它是Java 10中的一个bug吗?

评论的后续内容:

  • 在使用Java9或更高版本编译时出现该问题(我们在Java10中发现了该问题)。在Java8上编译这段代码,然后在Java9或任何更高版本中运行,包括Java11早期访问,就会得到预期的结果。

  • 这类代码是非标准的,但根据规范是有效的。它是由Kevin Cruijssendiscussion in a 中发现的,因此出现了奇怪的用例encountered.

  • Didier L用更小、更容易理解的代码简化了这个问题:

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

Result when compiled in Java 8:

已评估

Result when compiled in Java 9 and 10:

评估评估的

  • 该问题似乎仅限于字符串连接和赋值运算符(+=),以及带有副作用的表达式作为左操作数,如在array[test()]+="a"array[ix++]+="a"test()[index]+="a"test().field+="a"中。若要启用字符串连接,两边中至少必须有一方的类型为String。尝试在其他类型或构造上重现此内容失败。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 02:26:06

这是从JDK9开始的javac中的一个bug (在字符串连接方面做了一些更改,我怀疑这是问题的一部分),as confirmed by the javac team under the bug id JDK-8204322。如果您查看行的相应字节码:

代码语言:javascript
复制
array[i++%size] += i + " ";

它是:

代码语言:javascript
复制
  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是来自阵列的实际负载。然而,这部分

代码语言:javascript
复制
  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

它大致相当于表达式array[i++%size] (减去实际的加载和存储),在那里出现了两次。正如规范在jls-15.26.2中所说的,这是不正确的

形式为E1 op= E2的复合赋值表达式等同于E1 = (T) ((E1) op (E2)),其中TE1的类型,不同之处在于只计算一次。

因此,对于表达式array[i++%size] += i + " ";,部分array[i++%size]应该只计算一次。但是它被评估了两次(一次用于加载,一次用于存储)。

所以,是的,这是一个bug。

一些更新:

该错误已在JDK11中修复,并已向后移植到JDK10 (herehere),但自it no longer receives public updates以来未移植到JDK9。

Aleksey Shipilev在JBS page上提到(在这里的评论中提到@DidierL ):

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

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

票数 637
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50683786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档