JAVA6, use string plus action __java

Source: Internet
Author: User
Tags stringbuffer

From the day Java was born, a bit of "some level" programmers were told that if there is a large number of string links, do not add strings, but instead use the

StringBuffer Append operation then ToString ().

To the JAVA5, we were told that StringBuilder is a stringbuffer version, so most of the time we can use StringBuilder

To replace the plus operation. Because a stringbuffer operation of an instance field that may be accessed by multithreading is rare, less than 1% of the string links are invoked in a method.

But to the JAVA6, because the compiler "always" compiles the string's + link into StringBuilder, so in 99% of cases, we should use the plus operation.

The reasons are:

1. Add operation than other operations to look intuitive, write easy, JAVA6 before, even if we know there are performance problems we will use the + link at some time, the root cause of course is

This kind of writing is intuitive and simple.

String str = "Axman" + "is" + "a great lion.";

StringBuilder sb = new StringBuilder ();

Sb.append ("Axman")

. Append ("is")

. Append ("A great lion.");

String str = sb.tostring ();

Who wants to say that the second way of trial is beautiful and written. I'm going to slap him in the face. Hit him until his brain is normal.

2. We also teach ourselves not to use stringbuffer on our own, while JAVA6 always optimizes the first form as StringBuilder and performs better in 99%, unless you need an instance field that is accessed by multiple threads.

Use concat when only two strings are connected.

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.