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
15
u/f03nix Jun 05 '18
Actually += is an operator by itself, there's no reason it should expand to that. Coming from a c++ background - I'd expect this to only evaluate once. In my head I read y += x as {y} = {y} + {x} where {y} and {x} are evaluation results of the expression. This is super important because otherwise every operation of the form *x+= y would mean x being dereferenced twice.
This is also true in the case of x++, while it can be written as x = x + 1, it shouldn't be expanded like that. Otherwise you'll never be able to use something like pressed[NextChar()]++; .
They're convinced it's an error because for all other array types, this is evaluated only once.