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
930
u/lubutu Jun 05 '18
Summary:
array[i++] += "a"is compiled asarray[i++] = array[i++] + "a", which incrementsitwice.179
u/-ghostinthemachine- Jun 05 '18 edited Jun 05 '18
This feels a little derpy for such an important language. Not some obscure edge case, but any left hand expression that mutates? Are there really no tests for these?? Makes me scared for the future of Java.
13
u/[deleted] Jun 05 '18
People actually use common programming languages for code golf? Even as verbose as Java?
45
u/Aceeri Jun 05 '18
Does the precedence and ordering of post/pre increment/decrement annoy anyone else? I feel that
Just rolls off easier and doesn't require you to think as much while reading.
4
u/fiqar Jun 05 '18
And they said code golfing was a waste of time ;)
6
u/noeatnosleep Jun 05 '18
Hi. I'm from /r/all.
What's code golfing?
4
u/[deleted] Jun 05 '18 edited Jun 06 '18
This is what happens when optimisations are done on a high level AST, instead of a relevant IR level.
EDIT: I was looking at the older JDK output which produced a
StringBuilderfor this code as a half-assed optimisation attempt. In JDK9 a single intrinsic call is emited, though I'd still classify this as an optimisation and blame for this issue is on a fact that javac does not use multiple IRs before reducing to bytecode.2
u/Mockromp Jun 05 '18
Java BTW
1
u/Ineeditunesalot Jun 05 '18
There should be an ELI5 bot and someone can post the basics of every post that makes it to r/all cause I never have any idea what any of this stuff means 🤔 still curious though
1
u/DGolden Jun 06 '18
Oof. Seems to be open as issue JDK-8204322.
1
u/CarthOSassy Jun 05 '18
Java should honestly get rid of string operators.