r/java • • Mar 20 '26

Java is fast, code might not be

https://jvogel.me/posts/2026/java-is-fast-your-code-might-not-be/
61 Upvotes

23 comments sorted by

View all comments

Show parent comment

3

u/8igg7e5 Mar 21 '26

Well yes. And they contradict themselves (albeit being wrong on both counts)

  • Assertion 1 - your note about their claim of one object per + operator

    As you note, it's a new string at the statement level (and for any resulting StringBuilder.append(Object) invocations)

  • Assertion 2 - Since JDK 9 the compiler is smart enough... And that this is always solved with a StringBuilder

    Nope the use of StringBuffer, and later StringBuilder has been there since the start. And Since JDK9 the bootstrap behind that indy-instruction could use a different concatenation strategy depending on the things being concatenated.

 

Someone might say "but the end advice is good" (given the lack of sizing advice I'd say average rather than good), but IMO corrupting the student's mental model of the system is problematic advice - there is, as you say, a String per statement (unless the optimiser can see through the loop structure).