Java String,stringbuffer and Stringbulder learning notes

Source: Internet
Author: User

1.String: non-changing sequence of Unicode characters.

Pooling ideas, put the data that needs to be shared in a pool, and use a storage area to store some common resources to reduce the overhead of storage space.

In the string class, when created in literal value, go back to the string pool of Java method space, and if not, create a string object in the pool and return the address assigned to the object variable, if any, return the address of the string in the pool and assign the address to the object variable. If new, a string class object is created in the heap space and there is no such procedure.

        String s1= "ABC"; // newly created, string constant pool is not in, then a string "abc"        is created in the pool String s2= "ABC"; // "ABC" already exists in the string pool, and S2 will point to "ABC" instead of creating a new one.         string s3=new string ("abc");   Open a new space directly in the heap, without going to the pool to find it. 

Calling a method in any string does not alter the string itself unless it is re-copied.

2.StringBuffer: A sequence of Unicode characters that can be changed. Allows concurrent operations, which are thread-safe.

The string class is inefficient when connecting strings because the properties of the object it produces cannot be modified, and only new objects can be created when the connection string is made.

For many string connections, you should use the StringBuffer class, which optimizes efficiency by not having extra intermediate objects generated when strings are concatenated by objects of this class.

        String str= "A" + "B" + "C" + "D"; // The " AB" generated in the string pool, "ABC" is obviously a waste of space for objects.                  String s=null;        StringBuffer SB=new stringbuffer ("A");        Sb.append ("B");        Sb.append ("C");        Sb.append ("D");        S=sb.tostring ();

3.StringBulder: A sequence of Unicode characters that can be changed. Operations with StringBuffer, which do not support concurrent operations, are not thread-safe.

Java String,stringbuffer and Stringbulder learning notes

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.