The Java Foundation String,stringbuilder,stringbuffer

Source: Internet
Author: User

It is always customary to use string str = "..." When creating strings, and str = str+ "ABCD"; I have heard of Stringbuffer,stringbuilder, but I have never thought about it.

String at first glance is very convenient to create, in fact, when the string is more or the creation of multiple strings, there will be a big problem, because string in the new creation of the string is in the memory of a new memory to store this string, because string is static, The reason we look dynamic is because the JVM deceives us, and in memory there is a new object, so the speed of its operation is conceivable. But StringBuffer and StringBuilder are not the same.

StringBuffer and StringBuilder are dynamic, StringBuffer buffer= new StringBuffer ("StringBuffer"); Buffer.append ("ABCD"); The new StringBuffer object was not created during this process. So their reading and writing speed is basically 10 times or dozens of times times that of a string.

The difference between StringBuffer and stringbuild is that one thread-safe one is insecure, StringBuffer is thread-safe, and StringBuilder is thread insecure. Just like Hashtable and HashMap, they were used on different occasions. Stringbuffered use in multi-threading to ensure the security of synchronized data, StringBuilder use in a single thread to ensure the rapid reading and writing.

The Java Foundation String,stringbuilder,stringbuffer

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.