Since Strings in Java are immutable, I’ve always used StringBuilder or StringBuffer to concatenate Strings. Does the String.format() method handle this issue as well as StringBuilder or StringBuffer? In other words, does String.format() manage memory as well as StringBuffer or StringBuilder?
Since Strings in Java are immutable, I’ve always used StringBuilder or StringBuffer to concatenate
Share
Based on the source code of Oracle JDK, it seems that the implementation creates a new
Formatterfor eachString#formatcall which in turn allocates a freshStringBuilderfor each call. So yes. But as mentioned by the comment to your question, this is very much implementation specific though common sense entails that it would choose the most efficient way of doing things.