String,stringbuffer,stringbuilder and differences in Java

Source: Internet
Author: User

1.String

The string class in Java is immutable, which is final in the source code of the Declaration, so it can only be declared once.
So each change on the materializing is actually a string object that is regenerated, and the pointer points to the new string object.
At the same time, the string internally overrides the equal method, the original object equal is two objects equal can be,
But now, not by the object equality to judge the value is equal, the rewritten equal will compare characters,
This is why you should use equal for the same content string.

At the same time string a= "111" + "222" + "333";
With String b= "111";
String c= "222";
String d= "222";
String a=b+c+d;
The overhead is not the same, obviously faster on the above, string is the fastest way to build strings

The latter is in fact called the Append method of StringBuffer, one by one added.
The former, when compiled, is 111222333.

2.StringBuffer
StringBuffer is thread-safe. A string of character buffers, if you want to make frequent character changes,
Using this is the best choice, and each action is the operation of its own object, and it is separated from the string class.
The most used is append and insert,append are inserted at the end of the string, insert is inserted into the specified position.


3.StringBuilder
But not every time a lot of threading and frequent operations, but can not continue to create new objects with string impact performance.
So there's a StringBuilder. APIs are compatible with StringBuffer, and are most suitable for single-threaded operations.



Three uses:
1.String is suitable for a small amount of data, StringBuffer is suitable for multi-threaded frequent operation of large amounts of data, StringBuilder for
Single-threaded operation of large amounts of data.
2. Do not often use string concatenation + number, because each time a new object is created to increase the cost, such as the above string a=b+c+d;
3.StringBuffer and StringBuilder are best created when the length is defined, which improves performance better.
There is a difference between the operation of the system, multithreading or single-threaded, so that according to the characteristics of StringBuffer and StringBuilder to choose a better way

String,stringbuffer,stringbuilder and differences in Java

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.