开发者

In Java, is the immutability of Strings considered in the implementation of String.format()?

开发者 https://www.devze.com 2023-03-20 06:10 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消