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/evaned Jun 06 '18
It's not an implementation detail though.
Incrementally appending to a string (if the compiler didn't or doesn't optimize it into a
StringBuilder) is O(n^2) as a result of this. By comparison, incrementally appending to astd::stringin C++ isO(n). (nis the number of appends.)Or take a visible aspect:
no matter what happens in the ellipsis, you know
s2will not change, andprintln(s2)will printfoo. That's because of a combination of these things: (1)s2itself isn't changed to point at another object because it's never mentioned (and Java provides no other way to do it), and (2) the string it points to can't be changed.By contrast:
that prints 1, because now
a2is the list[5].(The above may be almost-Java; it's been a while since I wrote any.)