String string concatenation inefficient, you know why?

Why String with a "+" string concatenation inefficient, it is best to talk about it from the JVM angle?
For this issue, we take a look at the following code:

public class StringTest {
  public static void main(String[] args) {
  String a = "abc";
  String b = "def";
  String c = a + b;
  String d = "abc" + "def";
  System.out.Println(c);
  System.out.Println(d);
  }
}

Print Results:

abcdef
abcdef

From the above code example, we see the results in two ways stitching string printed is the same. But this is only the actual internal operation is not the same on the surface.

Both what is different?
String c = a + b; " +" is equivalent to a new StringBuilder, and then calls the initialization method of StringBuilder, then append operation, and finally toString ();
String D = "ABC" + "DEF"; two constants peer occurs, JVM at compile time, it considers the "+" is useless, directly into String d = "abcdef" compile time;

So efficiency where to start?
In fact, the above two examples, the connection string expressions are simple line, then the "+" and StringBuilder basically the same, but if the structure is more complex, such as used to connect the string loop, so produced will have very Java Byte Code big difference. Let us look at the following piece of code:

import java.util.*;
public class StringTest {
     public static void main(String[] args){
          String s = "";
          Random rand = new Random();
         for (int i = 0; i < 10; i++){
              s = s + rand.nextInt(1000) + " ";
          }
          System.out.println(s);
      }
}

From the above, "+" inside the for loop, each execution will create a StringBuilder, although Java has a garbage collector, collector but working hours are not fixed, if the continuous generation of waste, will take up a lot of resources. Therefore, the program should be used to connect directly StringBuilder string, as follows:

import java.util.Random;
public class StringTest {
    public static void main(String[] args) {
        Random rand = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            result.append(rand.nextInt(1000));
            result.append(" ");
        }
        System.out.println(result.toString());
    }
}

As can be seen from the above results, decompile, create StringBuilder code is placed outside the for statement. Although this process seems complicated in the source, but in exchange for greater efficiency, while consuming fewer resources also.

Therefore, from the above several examples, we conclude that: String using concatenation operator (+) inefficient, both the circulation, where large quantities of data resulting from each do a "+" to produce a StringBuilder object, then append after the throw. Regenerating a StringBuilder object before reaching the next cycle, and then append a string, and so on until the end. If we adopt the direct object StringBuilder append, we can save time creating and destroying objects. If you simply splice or rarely literal string stitching, performance are similar.

Published 30 original articles · won praise 12 · views 3465

Guess you like

Origin blog.csdn.net/zx1293406/article/details/103548547