Since Strin开发者_如何学Gogs 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?
Based on the source code of Oracle JDK, it seems that the implementation creates a new Formatter
for each String#format
call which in turn allocates a fresh StringBuilder
for 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.
精彩评论