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.