[Java] Implementation principle and efficiency of "+" in string

Source: Internet
Author: User

The operation of string in Java is often related to the connector "+", for example, we can use string = int + "" to convert an int integer or other base type to a string type, or you can concatenate two strings with string = string + string. So how exactly are these connectors implemented? And what about their efficiency?

First we can look at the API documentation:

The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings. string concatenation is implemented through the StringBuilder (or StringBuffer) class and its append methods. String conversions are implemented by the ToString method, which is defined by the Object class and can be inherited by all classes in Java. For more information about string concatenation and conversion, see the Java Language specification co-authored by Gosling, Joy, and Steele.

Is that what it says? Decide to verify with decompile that the code is as follows:

public class Test {public    static void Main (string[] args) {        int i = ten;        String s = "abc";        SYSTEM.OUT.PRINTLN (s + i);}    }
The post-compilation code becomes:

public class Test {public    static void Main (String args[]) {    //deleted default constructor and byte code byte        byte0 = ten;              String s = "abc";              System.out.println ((New StringBuilder ()). Append (s). Append (Byte0). toString ());}    }
So we can clearly see that the original Java "+" connection string object, will create a StringBuilder () object and call the Append () method to splice the data, and finally call the ToString () method to return the concatenated string, because append () The various overloaded forms of the method call the String.valueof method, so we can assume that:

The following two are equivalent s = i + "" s = string.valueof (i); The following two are also equivalent s = "abc" + i;s = new StringBuilder ("abc"). Append (i). toString ();
The way this JVM implicitly creates StringBuilder is not a loss of efficiency in most cases, but you need to be aware of the large number of looping stitching strings, such as the source code:

</pre><pre name= "code" class= "Java" >public class Test {public    static void Main (string[] args) {        String s = "abc";        for (int i=0; i<10000; i++) {            s + = "abc";}}    }
The post-compilation code is:

public class Test {public    static void main (string args[]) {        string s = "abc";        for (int i = 0; i < i++) {            s = (new StringBuilder ()). Append (s). Append ("abc"). toString ();}}        }
This will certainly result in a loss of efficiency due to the large number of StringBuilder created in heap memory, so in this case it is recommended to create a StringBuilder object call outside the loop body append () Method Manual stitching (as in the example above if the use of manual splicing run time will be reduced to about 1/200). In addition to this, there is a special case, that is, when the "+" both ends of the string constant (refers to "ABC" and so on instead of the final decoration of the string object), after compilation will be directly spliced, for example:

public class Test {public    static void Main (string[] args) {        System.out.println ("Hello" + "World");}    }
Post-compilation becomes:
public class Test {public    static void Main (String args[]) {        System.out.println ("HelloWorld");}    }
The efficiency of such a situation is certainly the best, but generally no one will use "+" splicing two string constants bar.

The final conclusion: in most cases, the use of "+" connection string will not cause loss of efficiency, but also improve the readability and simplicity of the program, not to worry about even if the use of it!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.