Difference between String, StringBuffer and StringBuilder

I recently learned about StringBuffer, and I have some questions in my heart. I searched for some things about String, StringBuffer, and StringBuilder, and now I will sort it out.

The position of these three classes in string processing is self-evident, so what are their advantages and disadvantages, and when should you use them? Let us explain the following points

  1. Comparison of the execution speed of the three: StringBuilder > StringBuffer > String

  2. Reason for String < (StringBuffer, StringBuilder)

    String: String constant

    StringBuffer: String variable

    StringBuilder: String variable

    As you can see from the above name, String is a "string constant", that is, an immutable object. For the understanding of this sentence, you may have such a question, such as this code:

1  String s  = "abcd";2= s+1;3System.out.print(s);// result : abcd1  
 
 

 

       We obviously changed the variable s of String type. Why do we say it has not changed? In fact, this is a kind of deception. The JVM parses this code like this: first create the object s, assign an abcd, and then create a new one The object s is used to execute the second line of code, which means that the object s has not changed before, so we say that the String type is an immutable object. Because of this mechanism, whenever a string is manipulated with String, it is actually New objects are constantly being created, and the original objects will become garbage and be reclaimed by the GC. It is conceivable how much the execution efficiency will be.

     And StringBuffer is different from StringBuilder. They are string variables and objects that can be changed. Whenever we use them to operate on strings, we actually operate on an object, so it will not be like String. Create some external objects to operate, of course, the speed is faster.

  3. A special example:

1  String str  =  “This is only a”  +  “ simple”  +  “ test”; 3StringBuffer builder =new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);
   

 

  

    You will be surprised to find that the speed of generating str objects is simply too fast, and at this time StringBuffer has no advantage in speed at all. It's actually a trick of the JVM , actually:

    String str = “This is only a” + “ simple” + “test”;

    In fact it is:

    String str = “This is only a simple test”;

    So it doesn't take much time. But what everyone should pay attention to here is that if your string is from another String object, the speed is not so fast, for example:

    String str2 = “This is only a”;

    String str3 = “ simple”;

    String str4 = “ test”;

    String str1 = str2 +str3 + str4;

    At this time, the JVM will do it in the original way.

  4.StringBuilder与 StringBuffer

    StringBuilder: thread-unsafe

    StringBuffer: thread-safe

    When we use the string buffer to be used by multiple threads, the JVM cannot guarantee that the operation of StringBuilder is safe. Although it is the fastest, it can guarantee that StringBuffer can operate correctly. Of course, in most cases, we operate under a single thread, so in most cases, it is recommended to use StringBuilder instead of StringBuffer, which is the reason for speed.

 

           For the summary of the use of the three : 1. If you want to operate a small amount of data, use = String

                        2. Operate a large amount of data under a single-threaded string buffer = StringBuilder

                        3. Multi-threaded operation of a large amount of data under the string buffer = StringBuffer

 

          偶是初学者,只是总结了自己学习的东西,难免写的东西里有别人的,学习是记忆的过程,这些东西只是用来学习罢了,有些东西可能不对,希望各位给予指正。

转自http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520098&siteId=291194637