为什么 array[idx++]+="a" 在 Java 8 中增加一次 idx 而在 Java 9 和 10 中增加两次?
Why does array[idx++]+="a" increase idx once in Java 8 but twice in Java 9 and 10?
挑战,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:
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:
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 中的错误吗?
跟进评论:
使用 Java 9 或更高版本编译时会出现此问题(我们在 Java 10 中发现了它)。在 Java 8 上编译此代码,然后在 Java 9 或任何更高版本中编译 运行,包括 Java 11 抢先体验,给出预期的结果。
这种代码是非标准的,但根据规范是有效的。它是由 Kevin Cruijssen in a discussion in a golfing challenge 发现的,因此遇到了奇怪的用例。
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:
evaluated
Result when compiled in Java 9 and 10:
evaluated
evaluated
问题似乎仅限于字符串连接和赋值运算符 (+=
),其左操作数的表达式具有副作用,如 array[test()]+="a"
、array[ix++]+="a"
、test()[index]+="a"
或 test().field+="a"
。要启用字符串连接,至少一侧的类型必须为 String
。尝试在其他类型或构造上重现此失败。
这是 javac
中的一个错误,从 JDK 9 开始(它对字符串连接做了一些更改,我怀疑这是问题的一部分),as confirmed by the javac
team under the bug 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
大致对应于表达式array[i++%size]
(减去实际加载和存储),在其中两次。这是不正确的,正如 jls-15.26.2:
中的规范所述
A compound assignment expression of the form E1 op= E2
is equivalent to E1 = (T) ((E1) op (E2))
, where T
is the type of E1
, except that E1
is evaluated only once.
因此,对于表达式 array[i++%size] += i + " ";
,array[i++%size]
部分应该只计算一次。但是它被评估了两次(一次用于加载,一次用于存储)。
所以是的,这是一个错误。
一些更新:
该错误已在 JDK 11 中修复,并已向后移植到 JDK 10(here and here), but not to JDK 9, since it no longer receives public updates.
Aleksey Shipilev 在 JBS page 中提到(以及此处评论中的@DidierL):
Workaround: compile with -XDstringConcat=inline
这将恢复为使用 StringBuilder
进行连接,并且没有错误。
挑战,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:
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:
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 中的错误吗?
跟进评论:
使用 Java 9 或更高版本编译时会出现此问题(我们在 Java 10 中发现了它)。在 Java 8 上编译此代码,然后在 Java 9 或任何更高版本中编译 运行,包括 Java 11 抢先体验,给出预期的结果。
这种代码是非标准的,但根据规范是有效的。它是由 Kevin Cruijssen in a discussion in a golfing challenge 发现的,因此遇到了奇怪的用例。
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:
evaluated
Result when compiled in Java 9 and 10:
evaluated evaluated
问题似乎仅限于字符串连接和赋值运算符 (
+=
),其左操作数的表达式具有副作用,如array[test()]+="a"
、array[ix++]+="a"
、test()[index]+="a"
或test().field+="a"
。要启用字符串连接,至少一侧的类型必须为String
。尝试在其他类型或构造上重现此失败。
这是 javac
中的一个错误,从 JDK 9 开始(它对字符串连接做了一些更改,我怀疑这是问题的一部分),as confirmed by the javac
team under the bug 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
大致对应于表达式array[i++%size]
(减去实际加载和存储),在其中两次。这是不正确的,正如 jls-15.26.2:
A compound assignment expression of the form
E1 op= E2
is equivalent toE1 = (T) ((E1) op (E2))
, whereT
is the type ofE1
, except thatE1
is evaluated only once.
因此,对于表达式 array[i++%size] += i + " ";
,array[i++%size]
部分应该只计算一次。但是它被评估了两次(一次用于加载,一次用于存储)。
所以是的,这是一个错误。
一些更新:
该错误已在 JDK 11 中修复,并已向后移植到 JDK 10(here and here), but not to JDK 9, since it no longer receives public updates.
Aleksey Shipilev 在 JBS page 中提到(以及此处评论中的@DidierL):
Workaround: compile with
-XDstringConcat=inline
这将恢复为使用 StringBuilder
进行连接,并且没有错误。