Why does `array[i++%n] += i+“ ”` give different results in Java 8 and Java 10?

  • A+
Category:Languages

For a challenge, someone 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  

The numbering is entirely off using Java 10. Even though it looks like i is properly computed.

Note that it's not a reference issue since the array contains 3 different strings everytime.

So what is happening here? Is it a bug in Java 10?

 


This seems to be a bug in javac starting from JDK 9 (which made some changes with regard to string concatenation, which I suspect is part of the problem), if you look at the corresponding byte code for the line:

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

Part of it is:

  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 

Where the last aaload is the actual load from the array. However, the part

  21: aload_2   22: iload_3   23: iinc          3, 1   26: iload_1   27: irem 

Which roughly corresponds to the expression i++%size, is in there twice. This is incorrect, as the spec says in 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.

So for the expression array[i++%size] += i + " ";, the part array[i++%size] should only be evaluated once. But it is evaluated twice (once for the load, and once for the store).

So yes, this seems to be a bug.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: