Does "+" use string efficiency in concatenation?

I worked with String, StringBuilder and StringBuffer in java.
I thought about this question while I was thinking about efficiency.

Does "+" use string efficiency in concatenation?

+4
source share
6 answers

Yes, but so few that it doesn’t matter in most cases.

Using '+' for string constants is most effective since the compiler can perform concatenation.

If you join two strings, the concat method is the most efficient, since it avoids using StringBuilder.

There was almost never any reason to use StringBuffer other than backward compatibility. StringBuilder or StringWriter is the best choice. However, it is still used explicitly more often than StringBuilder in the JDK: P

StringBuffer is dead, long live StringBuffer

+5
source

If you merge into a single statement, it does not matter, since the compiler / JIT compiler automatically optimizes it using StringBuilder .

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

However, if you concatenate a large number of String in a loop, definitely use StringBuilder , as this will speed up your program significantly.

 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(); 
+4
source

A bit Yes, but still NO

From JLS, 15.18.1.2

String Concatenation Optimization

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

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

+1
source

In your example:

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

we must have string strings that can be replaced by the compiler, so this will be faster than StringBuffer / append / toString.

But more efficient / faster than what? Code execution? Spelling code Reading code?

Since reading

 "Foo = " + foo; 

very easy, I would recommend it if it does not repeat a million times, or "s + = s2;" repeated the hundret once.

In particular,

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

much more readable than

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

Just avoid it in applications that require high performance, or combine a very large number of lines or a large amount recursively.

+1
source

Because strings are immutable, string concatenation causes the creation of a string. those. "A" + "B" + "C" causes the creation of "AB", then "ABC". If you perform multiple concatenations, it will always be more efficient to use a StringBuilder (rather than an old, but Api-like StringBuffer, which has the cost of synchronization)

If you have one string concatenation, the compiler will do it for you, if possible, that is, if you compile "A" + "B" + "C", you will most likely see if you decompile something sort of like a new StringBuilder ("A"). append ("B"). append ("C"). toString (). However, the compiler cannot optimize multiple concatenation of strings over several lines - i.e. If you have multiple concatenation lines, you'll see something like the above on each line, including the optional creation of a StringBuilder. It’s better to do it manually.

You can verify this yourself by collecting a simple example and decompiling.

-1
source

If you use multiple concatenation with '+', then yes to some extent. Goats, when you do String a + String b, it actually internally creates a StringBuffer object and uses append () for the StringBuffer. Therefore, each time you do "+", a new temporary StringBuffer object is created, initialized with "a", and then added with "b", which is then converted to a string object.

So, if you need some concatenations, you have to create a StringBuffer (thread safe) / StringBuilder (not thread safe) object and keep adding so that you avoid creating StringBuffer objects again and again.

-1
source

All Articles