How two strings in Java are implemented by "+" __java

Source: Internet
Author: User
Tags stringbuffer

We know that once you declare a variable of type string, it points to an object, and when you modify the variable, the variable points to a new object. But like StringBuffer, it doesn't produce new objects:

StringBuffer sb = new StringBuffer ();
Sb.append ("abc"). Append ("Def");
Here SB will always point to the same object. And we use "+" to stitch up two strings, such as String str = STR1+STR2 do the following:

Call string.valueof (obj), where obj is a String-type variable, as the str1 and str2,string.valueof () mentioned above are: return OBJ = null? Null ": obj.tostring (); generates StringBuilder, invokes StringBuilder (STR1) Construction method, initializes StringBuilder, Str1.length+16, and invokes append (str1); Continue to invoke append (STR2); finally call Stringbuilder.tostring () return the result so the final conclusion should be: Stringbuilder.append (str1). Append (str2). ToString ().
If there are any errors, please also point out that learn from each other.



This article refers to: http://blog.csdn.net/zolalad/article/details/30089327


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.