last day (142 days later) » 

11:22 PM
62
Q: Why does `array[i++%n] += i+" "` give different results in Java 8 and Java 10?

Olivier GrégoireFor 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 + " "; } ...

 
Sidenote: the "wrong" output also appears with JRE/JDK 9.0.4
 
It seems the i++ and the += are evaluated twice in JDK 9/10, or something like that. Just print the whole array and the value of i after each iteration.
 
To try to isolate it: Does the jdk8-compiled code run correctly with a 9/10 jre? If so, that might suggest it's a javac bug (and not jvm).
 
I think I see the problem. @DidierL is right. the += seems to evaluate the expression i++%n twice, This leads to a) i incrementing by 2 in each iteration and b) the strange output since one entry from array is read, then i is appended to the read String and then the new String is assigned to the next entry of array. Looks like a bug to me. I am not sure whether it is the compiler or the runtime causing this, but I would think that this is a compiler issue.
 
It seems to be a javac bug. If you look at the byte code generated by JDK 10 javac, it does compute the remainder twice, which increments i twice in the process.
 
11:22 PM
It looks like there is a difference in the compiler, indeed. When I run the Java-8-compiled class in a Java-9-JRE, the result is the same as the Java-8-JRE (meaning I correctly get all the numbers accross all the lines).
 
If you expand the += to = array[i++%n] +, you get the same result in Java 8.
 
Adding System.out.println(i); in the loop shows that there are 100 iterations in the Java-8-compiled code (both run under Java 8 and 9), but only 50 iterations in the Java-9-compiled code (and i always becomes i+2).
 
@OlivierGrégoire, you say it was for a challenge. Did the person intentionally write this code in order to get different results based on the version? That person may have additional knowledge on this issue then.
 
@DidierL No, it was a golfing challenge (which in ungolfed in the question to avoid unreadable code). The person asked me why they got different results.
I've created a bug report to Oracle.
 
Can be simplified to String[] array = {""}; array[test()] += "a"; with static int test() { System.out.println("evaluated"); return 0; }: prints evaluated twice.
 
user4602302
11:22 PM
i've got no jdk 8, but when i run the code you posted in eclipse photon rc2 with jdk 10.0.1, the result is that one, you posted for jdk 8
 
@Oleksandr The relevant spec says: "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 array[test()] should only be evaluated once.
 
Thanks @JornVernee! Could you write an answer stating that it's a bug given the spec?
 
@OlivierGrégoire Sure, I thought we already established that ;) In that case, can you link to the bug report you made? That seems like an important part of the answer.
 
@JornVernee As usual with Oracle, the bug report is not yet public. It will be once the bug is confirmed. I will gladly adapt the answer once I have that piece of information.
 
I still don't get how such a compiler bug got into Java 9, and is only discovered now.
 
11:22 PM
@DidierL Actually, I vaguely remember this being discussed in the mailing list fairly recently, but I couldn't find any bug report for it. I might be wrong though, since it still seems to be present in the JDK 11-ea+8 I have.
 
eis
@DidierL JDK 9 reached General Availability on 21 September 2017, so it has been around less than a year. The infamous sorting bug was there over two decades, for example.
 
A valid question, but note that your problem would have been avoided had you ensured that only one state change happens per statement (so the ++i and += would be in separate lines). Doing so would also make your code more readable.
 
@jpmc26 The question wrote that it was "for a challenge," and comments confirmed that it was a golfing challenge. Readability is not the point for those. :)
 
@jpmc26 This is from a golfing challenge. In golfing challenges, a[i++%n]+=i+" "; is better than int t = i++; a[t%n] += i + " ";, because it's shorter.
 
@eis Outside the GA that could highlight this kind of bugs in existing (dirty) code, I would also expect the JDK to have unit tests for this kind of corner cases (compound assignment to String array expression).
 
11:22 PM
@DidierL Thanks Olivier, Didier, Jorn, and others for tracking down and filing this bug. The javac team is looking at it now. It does indeed look like a current, previously unknown bug. I'll post with more info when I get it.
 
@Stuart, in addition to arrays, it seems you can also reproduce it with assigning to fields, like new MyObject().field += "a" or test().field += "a".
 
@DidierL I've updated the bug report and linked back to this question. I expect the bug report will become publicly visible in a day or so. Thanks for helping diagnose this!
 

  last day (142 days later) »