What is the difference between String, StringBuffer, StringBuilder is

Java is used for handling strings have three classes:

1、java.lang.String

2、java.lang.StringBuffer

3、java.lang.StrungBuilder

A, Java String class --String string constants

String widely used in the Java programming in Java strings belong to the object, Java provides the String class to create and manipulate strings.

It should be noted that the value of String is immutable, which leads generated each time a new String objects String operations will, not only inefficient, but also a lot of waste of the limited memory space. We look at this memory changes during operation of the String Figure:
  

  We can see that the initial value of String "hello", and then later in the string with a new string "world", the process is the need to re-open the memory space in the stack heap memory, finally got the "hello world "string is also a corresponding need to open up memory space, so that just two strings, it takes three times to open up memory space, have to say this is a waste of memory space. In response to frequent string related operations, we need to use two other operating string Java provides classes --StringBuffer class to class and StringBuild of such changes string processing.

Two, StringBuffer and StringBuilder class --StringBuffer, StringBuilder string variable

  • When the string changes, and requires the use of StringBuffer StringBuilder class.
  • And String class is different, and the StringBuffer StringBuilder class objects can be modified many times, and does not generate new unused objects.
  • StringBuilder class is presented in Java 5, the biggest difference between it and the StringBuilder StringBuffer that the method is not thread-safe (can not synchronize access).
  • Since the StringBuffer StringBuilder compared to the speed advantage, so in most cases recommend using the StringBuilder class. However, in the case of application requires thread-safe, you must use the StringBuffer class.

Inheritance structure of the three

  

Three differences :

(1) the difference between (mainly) on the modification of character

  • String: immutable string;
  • StringBuffer: string variable, low efficiency and thread-safe;
  • StringBuilder: variable sequence of characters, high efficiency and thread-safe.

(2) the difference between the initialization, String assignment can be empty, the latter not being given

. 1  ①String
 2   
. 3 the StringBuffer S = null ;   
 . 4   
. 5 the StringBuffer S = "ABC";   
 . 6   
. 7  ②StringBuffer
 . 8   
. 9 the StringBuffer S = null ; // results Warning: Null pointer Access: Result of The variable CAN BE only the this null AT LOCATION 
10   
. 11 the StringBuffer = S new new StringBuffer (); // StringBuffer object is an empty object 
12 is   
13 is StringBuffer = S new new StringBuffer ( "ABC"); // Create StringBuffer object with the content, the content object is the character string "

Three things in common: all final class, is not allowed to inherit, mainly from the performance and security considerations, because these classes are often used with, and taking into account the parameters of which are modified to prevent the influence of parameters to other applications.

String:

  1. Is a string constant, it can not be modified once created. Modifications have been around for a String object is to re-create a new object, and then save the new value into it.
  2. And String object rather than a basic type.
  3. covering string equals method and hashCode () methods.

StingBuffer:

  1. The object is a string variable, the string can be operated, a new object does not modify the original string value.
  2. Efficiency is slow, but the thread safety
  3. StringBuffer not override equals and hashCode () method.
  4. The character data may be dynamically configured, append () method.

StringBuilder:

  1. Also a string variable objects, like StringBuffer, can operate on strings, it will not create a new object.
  2. Efficiency and efficient, but not thread-safe.

String, StringBuilder, StringBuffer usage scenarios are as follows:

       When the processing fixed-length string, String recommended;

       When the process variable-length string, and is single-threaded environment, the StringBuilder recommended;

       When the process variable-length strings, and when a multithreaded environment, the proposed StringBuffer.

Summary :

(1) If the amount of data to be operated with String;

Operation StringBuffer large amounts of data (2) multi-threaded operating string buffer;

(3) single-threaded operating the operation amount of data the StringBuilder (recommended) the string buffer.

  

reference:

1 https://www.cnblogs.com/jasonboren/p/11053044.html

2、https://blog.csdn.net/weixin_41101173/article/details/79677982

Java is used for handling strings have three classes:

1、java.lang.String

2、java.lang.StringBuffer

3、java.lang.StrungBuilder

A, Java String class --String string constants

String widely used in the Java programming in Java strings belong to the object, Java provides the String class to create and manipulate strings.

It should be noted that the value of String is immutable, which leads generated each time a new String objects String operations will, not only inefficient, but also a lot of waste of the limited memory space. We look at this memory changes during operation of the String Figure:
  

  We can see that the initial value of String "hello", and then later in the string with a new string "world", the process is the need to re-open the memory space in the stack heap memory, finally got the "hello world "string is also a corresponding need to open up memory space, so that just two strings, it takes three times to open up memory space, have to say this is a waste of memory space. In response to frequent string related operations, we need to use two other operating string Java provides classes --StringBuffer class to class and StringBuild of such changes string processing.

Two, StringBuffer and StringBuilder class --StringBuffer, StringBuilder string variable

  • When the string changes, and requires the use of StringBuffer StringBuilder class.
  • And String class is different, and the StringBuffer StringBuilder class objects can be modified many times, and does not generate new unused objects.
  • StringBuilder class is presented in Java 5, the biggest difference between it and the StringBuilder StringBuffer that the method is not thread-safe (can not synchronize access).
  • Since the StringBuffer StringBuilder compared to the speed advantage, so in most cases recommend using the StringBuilder class. However, in the case of application requires thread-safe, you must use the StringBuffer class.

Inheritance structure of the three

  

Three differences :

(1) the difference between (mainly) on the modification of character

  • String: immutable string;
  • StringBuffer: string variable, low efficiency and thread-safe;
  • StringBuilder: variable sequence of characters, high efficiency and thread-safe.

(2) the difference between the initialization, String assignment can be empty, the latter not being given

. 1  ①String
 2   
. 3 the StringBuffer S = null ;   
 . 4   
. 5 the StringBuffer S = "ABC";   
 . 6   
. 7  ②StringBuffer
 . 8   
. 9 the StringBuffer S = null ; // results Warning: Null pointer Access: Result of The variable CAN BE only the this null AT LOCATION 
10   
. 11 the StringBuffer = S new new StringBuffer (); // StringBuffer object is an empty object 
12 is   
13 is StringBuffer = S new new StringBuffer ( "ABC"); // Create StringBuffer object with the content, the content object is the character string "

Three things in common: all final class, is not allowed to inherit, mainly from the performance and security considerations, because these classes are often used with, and taking into account the parameters of which are modified to prevent the influence of parameters to other applications.

String:

  1. Is a string constant, it can not be modified once created. Modifications have been around for a String object is to re-create a new object, and then save the new value into it.
  2. And String object rather than a basic type.
  3. covering string equals method and hashCode () methods.

StingBuffer:

  1. The object is a string variable, the string can be operated, a new object does not modify the original string value.
  2. Efficiency is slow, but the thread safety
  3. StringBuffer not override equals and hashCode () method.
  4. The character data may be dynamically configured, append () method.

StringBuilder:

  1. Also a string variable objects, like StringBuffer, can operate on strings, it will not create a new object.
  2. Efficiency and efficient, but not thread-safe.

String, StringBuilder, StringBuffer usage scenarios are as follows:

       When the processing fixed-length string, String recommended;

       When the process variable-length string, and is single-threaded environment, the StringBuilder recommended;

       When the process variable-length strings, and when a multithreaded environment, the proposed StringBuffer.

Summary :

(1) If the amount of data to be operated with String;

Operation StringBuffer large amounts of data (2) multi-threaded operating string buffer;

(3) single-threaded operating the operation amount of data the StringBuilder (recommended) the string buffer.

  

reference:

1 https://www.cnblogs.com/jasonboren/p/11053044.html

2、https://blog.csdn.net/weixin_41101173/article/details/79677982

Guess you like

Origin www.cnblogs.com/116970u/p/11495036.html