Why to use StringBuffer in Java instead of the str

2019-01-04 23:12发布

Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for Strings. What happens under the hood when you do that? What does StringBuffer do differently?

18条回答
闹够了就滚
2楼-- · 2019-01-04 23:35

StringBuffer is mutable. It adds the value of the string to the same object without instantiating another object. Doing something like:

myString = myString + "XYZ"

will create a new String object.

查看更多
冷血范
3楼-- · 2019-01-04 23:37

I think the simplest answer is: it's faster.

If you really want to know all the under-the-hood stuff, you could always have a look at the source yourself:

http://www.sun.com/software/opensource/java/getinvolved.jsp

http://download.java.net/jdk6/latest/archive/

查看更多
Root(大扎)
4楼-- · 2019-01-04 23:40

Because Strings are immutable, each call to the + operator creates a new String object and copies the String data over to the new String. Since copying a String takes time linear in the length of the String, a sequence of N calls to the + operator results in O(N2) running time (quadratic).

Conversely, since a StringBuffer is mutable, it does not need to copy the String every time you perform an Append(), so a sequence of N Append() calls takes O(N) time (linear). This only makes a significant difference in runtime if you are appending a large number of Strings together.

查看更多
再贱就再见
5楼-- · 2019-01-04 23:41

AFAIK it depends on version of JVM, in versions prior to 1.5 using "+" or "+=" actually copied the whole string every time.

Beware that using += actually allocates the new copy of string.

As was pointed using + in loops involves copying.

When strings that are conactenated are compile time constants there concatenated at compile time, so

String foo = "a" + "b" + "c";

Has is compiled to:

String foo = "abc"; 
查看更多
Bombasti
6楼-- · 2019-01-04 23:41

To concatenate two strings using '+', a new string needs to be allocated with space for both strings, and then the data copied over from both strings. A StringBuffer is optimized for concatenating, and allocates more space than needed initially. When you concatenate a new string, in most cases, the characters can simply be copied to the end of the existing string buffer.
For concatenating two strings, the '+' operator will probably have less overhead, but as you concatenate more strings, the StringBuffer will come out ahead, using fewer memory allocations, and less copying of data.

查看更多
家丑人穷心不美
7楼-- · 2019-01-04 23:43

The StringBuffer class maintains an array of characters to hold the contents of the strings you concatenate, whereas the + method creates a new string each time its called and appends the two parameters (param1 + param2).

The StringBuffer is faster because 1. it might be able to use its already existing array to concat/store all of the strings. 2. even if they don't fit in the array, its faster to allocate a larger backing array then to generate new String objects for each evocation.

查看更多
登录 后发表回答