In Java - immutability of strings examined in String.format () implementation?

Since strings in Java are immutable, I always used StringBuilder or StringBuffer to concatenate strings. Does the String.format () method use this problem, as well as StringBuilder or StringBuffer? In other words, does String.format () manage memory as well as StringBuffer or StringBuilder?

+6
source share
1 answer

Based on the Oracle JDK source code, 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 in the commentary on your question, this is a very concrete implementation, although common sense suggests that he will choose the most efficient way to do things.

+4
source

Source: https://habr.com/ru/post/892696/


All Articles