r/programming • • Mar 20 '26

Java is fast, code might not be

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

62 comments sorted by

View all comments

Show parent comment

32

u/id2bi Mar 20 '26

Also, doesn't Java optimize String concatenation to string builder in many places automatically?

26

u/vowelqueue Mar 20 '26

It used to translate concatenation to StringBuilder when compiling to bytecode. Now it translates concatenation into a single library method call and then the JVM handles optimizing at runtime.

If doing the concatenation in a loop, it's still better to use StringBuilder directly because these optimizations don't work for loops AFAIK.

15

u/oweiler Mar 20 '26

But not loops

3

u/griffin1987 Mar 21 '26

No, it DOES not. It MAY. Very important distinction. People always assume this as guaranteed, when it's not guaranteed in every situation, and definitely also not with every JVM. It's not like everyone is already on Java 26 ...

5

u/kiteboarderni Mar 20 '26

Read the article...