JDK 9/10/11: Side Effects from += on Java String
The question “Why does `array[i++%n] += i+” “` give different results in Java 8 and Java 10?” was posted earlier this week on StackOverflow.com. It points to a bug in the Java compiler that is present in JDK9 and later, but is not present in JDK8.
As explained on the StackOverflow thread, Didier L provided a simple example of Java code that reproduces this issue. That is adapted in the code listing shown next.
package dustin.examples.strings; import static java.lang.System.out; /** * Example demonstrating JDK-8204322 and adapted from Didier L's * original example (https://stackoverflow.com/q/50683786). */ public class StringConcatenationBug { static void didierLDemonstration() { final String[] array = {""}; array[generateArrayIndex()] += "a"; } static int generateArrayIndex() { out.println("Array Index Evaluated"); return 0; } public static void main(final String[] arguments) { didierLDemonstration(); } }
Reading the code shown above, one would expect to see the string “Array Index Evaluated” displayed once if this class’s main(String[])
function was executed. With JDK8, that was the case, but since JDK 9, it has not been the case. The next screen snapshot demonstrates this. The examples shown in the screen snapshot show that when the class is compiled with javac‘s -source
and -target
flags set to “8“, the string is shown only once when the compiled class is executed. However, when javac
‘s -source
and -target
flags are set to “9“, the string is shown twice when the compiled class is executed.
This bug exists in JDK9, JDK10, and JDK11. Olivier Grégoire has described this bug, “The issue seems to be limited to the string concatenation and assignment operator (+=
) with an expression with side effect(s) as the left operand.”
JDK-8204322 [“‘+=’ applied to String operands can provoke side effects”] has been written for this bug, has been resolved, and its resolution is targeted currently for JDK11. The bug report describes the problem, “When using the += operator, it seems that javac duplicates the code before the +=.” It also explains that code written like array[i++%n] += i + " ";
is compiled effectively to code like array[i++%n] = array[i++%n] + i + " ";
. Jan Lahoda’s comment on the bug describes why it occurs. Aleksey Shipilev has requested that this fix be backported to JDK 10 and it appears that it will be via JDK-8204340.
Additional background information regarding this bug can be found in the previously mentioned StackOverflow thread, in the related StackOverflow chat, and on the OpenJDK compiler-dev mailing list threads “Compiler bug about string concatenation” and “RFR: 8204322: ‘+=’ applied to String operands can provoke side effects“.
Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: JDK 9/10/11: Side Effects from += on Java String Opinions expressed by Java Code Geeks contributors are their own. |
That’s an interesting little bug, especially since it only applies to string concatenation. Java not having its own unique concatenation operator may cause problems like this one to be overlooked.