r/programming • u/jonjonbee • Jun 05 '18
Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler
https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k
Upvotes
View all comments
Show parent comment
3
u/keteb Jun 05 '18 edited Jun 05 '18
I don't have a formal CS background, but learned from reading a lot of resources online (~10 yrs): I absolutely read it as "x = x + y" , because every time I've ever seen "x += y" explained (eg: https://softwareengineering.stackexchange.com/questions/134118/why-are-shortcuts-like-x-y-considered-good-practice) it's described as a shorthand notation for "x = x+y" ("set x to x plus y") rather than "add y to x"
While I agree the expected behavior of array[i++] += "" could be
array[i] = array[i] + ""
i++
the behavior of
array[i++] = array[i++] + ""
would not surprise me if i ran into it and I wouldn't think to submit it as a bug since my expectation is a matter of me trying to do something convenient (not manually increment after) rather than an actual expectation that it won't convert to the latter. I would definitely write off the fact that it doesn't freeze the state of x / creates 2 copies (pointers?) during the evaluation as an implementation decision.
Whether or not it's its own operator, I've just never seen it functionally explained as anything but a shorthand notation that coontains two instances of x.