Analysis and comparison of Java--string, StringBuilder and StringBuffer?

Source: Internet
Author: User
Tags stringbuffer

Parsing of a string
Meaning of the 1.String
①string can not be inherited, the string class is the final class, and the string class is a char[] array to store strings.
②string is an immutable sequence of characters that, if stored in ABC, creates an array of characters with a fixed length of 3 in the string constant pool, regardless of how the change will result in a new instance.

Method of 2.String

The method of the known string, which is not modified on the basis of the original string, is the new instance, because string is an immutable sequence of characters. Any change to the Sring object does not change the original string.

Second, the concept of a string constant pool
1.String c = "abc" String cc = new String ("ABC") in memory distribution?

①sting c = "abc" is first found in the string constant pool, if not in the constant pool, instantiates the string and puts it in a constant pool, or, if ABC exists in the pool, assigns the address of the string directly to the ABC of C,C to the constant pool.
②string cc = new String ("ABC") first find ABC in the string constant pool, and if there is another space in the heap that points to ABC in the constant pool, the CC in the stack points to the 0x12 in the heap.
③ A total of 4 memory space, String cc = new String ("abc") If there is an ABC in the pool, create an object that creates 2 objects if there is no ABC in the pool. The execution order of
④string cc = new String ("Dec") is first right-to-left. First determine if Dec exists in the constant pool. If there is no instantiation of an object into the pool, then the new heap.
2. Description
① is not a new instance, and the result is true, which is a pointer to the string constant in the pool of 123.

②new instance, the result is false one pointing to the pool, one to the heap memory, and the address inconsistent.

③new Instance 2, the result is false, as long as the new out of the instance in memory will open up space, the address is inconsistent, so return false.

④ When a string is stitched together by multiple strings, it is itself a string constant.
New objects cannot be determined during compilation, and cz02 and cz03 can no longer be determined by the compiler. Both cz04 and cz05 point to heap memory, and the value of cz04 is determined when the program is run.
constant Find pool, variable find heap

⑤ compile-time optimizations, the JVM optimizes + connections to concatenated values, and its value is "A1" at compile time.

⑥ string constant concatenation and string reference concatenation, the constant "+" stitching is done at compile time, and string reference stitching ("+"), is determined when the program runs. One pointer to the string constant pool, one to the heap memory.

Iii. parsing and comparison of String, StringBuilder, StringBuffer
1.String Simple Summary
①string immutable sequence of characters
The object of ②new must be to create an object that opens up space in the heap.
③ directly assigns and new two ways to create objects of type string.
④ direct assignment does not necessarily create an object, and if there are words in the string constant pool, the instances in the direct heap point to the constant pool, and no objects need to be created.
⑤final decorated class, cannot be inherited.
⑥string a = "1" + "2" + "3" + "4"; This string stitching process produces multiple objects and is less efficient.
The difference between 2.String and StringBuilder, StringBuffer?
① variability: String immutable sequence of characters, builder and buffer are variable sequences of characters.
② Thread Safety: string is thread-safe, StringBuilder is thread insecure, StringBuffer is thread safe. Stringbuidler efficiency is higher than stringbuffer. Because string is immutable, it is generally the least efficient.


③ usage: If the string transform is less, use the string type, if the concatenation operation is more using StringBuilder, if the thread is required to use stringbuffer safely.
Parsing of 3.StringBuffer variable character sequences
① Initial capacity of 16



② automatic expansion: twice times the initial capacity plus 2

Analysis and comparison of Java--string, StringBuilder and StringBuffer?

Related Article

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.