Does “+” use in String concatenation affect effici

2019-01-20 12:19发布

I have worked with String, StringBuilder and StringBuffer in java.
I thought of this question, while I was thinking from efficiency point of view.

Does "+" use in String concatenation affect efficiency?

6条回答
做个烂人
2楼-- · 2019-01-20 12:36

In your example:

" Does +" + " use in String concatenation affect efficiency? "

we have to literal Strings, which might be replaced by the compiler, so this will be faster, than StringBuffer/append/toString.

But efficient/faster compared to what? Code execution? Code writing? Code reading?

Since reading a

"Foo = " + foo;

is very easy, I would recommend it, as long as it isn't repeated a million times, or a " s += s2;" repeated a hundret times.

Especially,

System.out.println ("Player " + n + " scores " + player[n].score); 

is far better readable than

System.out.println (new StringBuffer ("Player ").append ((Integer.valueOf (n)).toString ().append (" scores ").append (...

Just avoid it in applications which need high performance, or concatenate a very large amount of strings, or a large amount recursively.

查看更多
何必那么认真
3楼-- · 2019-01-20 12:37

If you are using multiple times concatenation with '+' , then yes to some extend. Coz when you do String a + String b , it actually internally creates a StringBuffer object and use append() of StringBuffer. So every time you do a '+' a new temporary StringBuffer object gets created initialized with "a" and then appended with "b", which then gets converted to a string object.

So if you need multiple concatenation you should rather create a StringBuffer(thread-safe)/StringBuilder(not thread safe) object and keep appending, so that you avoid the creation of StringBuffer objects again and again.

查看更多
够拽才男人
4楼-- · 2019-01-20 12:50

Since strings are immutable, concatenating strings causes a string create. ie.. "A"+"B"+"C" causes the creation of "AB", then "ABC". If you're doing multiple concatenations it will always be more efficient to use a StringBuilder (not the older but api-identical StringBuffer, which has synchronization costs)

If you have a single line concatenation the compiler will do this for you if possible - that is, if you compile "A"+"B"+"C" you're likely to see, if you decompile, something like new StringBuilder("A").append("B").append("C").toString(). However the compiler can't optimize multiple string concats over multiple lines - i.e, if you have multiple concatenating lines, you're going to see something like the above on each lines, including the extra StringBuilder creation. Better to do it manually.

You can verify this for yourself by putting together a simple example and decompiling.

查看更多
成全新的幸福
5楼-- · 2019-01-20 12:56

Yes, but so little it shouldn't matter most of the time.

Using '+' for string constants is the most efficient as the compiler can perform the concatenation.

If you are joining two Strings, the concat method is the most efficient as it avoids using a StringBuilder.

There is almost never a good reason to use StringBuffer except for backward compatibility. StringBuilder or StringWriter are a better choice. However, it is still used explicitly more often than StringBuilder in the JDK :P

StringBuffer is dead, long live StringBuffer

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-20 13:00

If you're concatenating in a single statement, then it won't matter since the compiler/JIT compiler will automatically optimize it using a StringBuilder.

So "a"+b+"c" will be optimized to (new StringBuilder("a").append(b).append("c")).toString()

However, if you're concatenating a large number of Strings in a loop, definitely explicitly use a StringBuilder as it will significantly speed up your program.

String a = "";

for( int i = 0; i < 1000000; i++ )
    a += i;

should be changed to

StringBuilder sb = new StringBuilder();

for( int i = 0; i < 1000000; i++ )
    sb.append(i);

String a = sb.toString();
查看更多
祖国的老花朵
7楼-- · 2019-01-20 13:03

A bit of Yes, But still NO

From the JLS, 15.18.1.2

Optimization of String Concatenation

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

查看更多
登录 后发表回答