Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - The difference between String class and StringBuffer class
The difference between String class and StringBuffer class
Differences in java First of all, there are two main differences between String and StringBuffer:

(1) String class objects are immutable. Once the value of the String object is modified, a new object is implicitly recreated and the original String object is released. The StringBuffer class object is modifiable. You can modify this value through the append () method.

(2) 2) The performance of String class objects is far less than that of StringBuffer classes.

The specific explanation is as follows:

There are three classes in java that are responsible for the operation of characters.

The 1. character operates on a single character.

2.String operates on strings. Immutable classes.

3.StringBuffer also operates on strings, but it is variable.

String:

The object is not of the original type.

As an immutable object, once created, its value cannot be modified.

Any modification to an existing string object is to create a new object and then save the new value.

String is the final class, that is, it cannot be inherited.

Stringbuffer District:

Is a mutable object, and when it is modified, it will not be recreated like a String.

It can only be established by the constructor,

string buffer sb = new string buffer();

Note: You can't assign a value to him through the assignment symbol.

Sb = "Welcome here!" ; //Error

After the object is created, memory space will be allocated in memory, and a null will be saved initially. To StringBuffer.

You can assign values through the append method.

Sb addition ("hello");

In String concatenation operation, StringBuffer is more effective than String:

String? str? =? New? String ("Welcome? Arrive? ”);

str? +=? "here"; In fact, the processing step is to create a StringBuffer for Hou to call append (). Finally,

Then string buffer to sting ();

In this case, the connection operation of String will have some extra operations than that of StringBuffer, and of course the efficiency will be reduced.

And because the String object is immutable, every time Sting is operated, a new object will be re-established to save the new value.

In this way, the original object is useless and will be recycled by garbage. This will also affect performance.

Please look at the following code:

26 English letters have been added 5000 times,

String? tempstr? =? ”abcdefghijklmnopqrstuvwxyz”;

int? Times? =? 5000;

Dragon? lstart 1? =? system . current time millis();

String? str? =? "";

For what? (int? Me? =? 0; ? Me? & lt? Times; ? i++)? {

str? +=? tempstr

}

Dragon? lend 1? =? system . current time millis();

Dragon? Time? =? (lend 1? -? lstart 1);

system . out . println(time); The result is not always, but usually around 1563.

Let's look at the following code again.

String? tempstr? =? ”abcdefghijklmnopqrstuvwxyz”;

int? Times? =? 5000;

Dragon? lstart2? =? system . current time millis();

StringBuffer? sb? =? New? string buffer();

For what? (int? Me? =? 0; ? Me? & lt? Times; ? i++)? {

sb . append(tempstr);

}

Dragon? lend2? =? system . current time millis();

Dragon? Time 2? =? (lend2? -? lstart 2);

system . out . println(time 2); The result is 16, sometimes 0.

So it is obvious that the speed of StringBuffer is almost 10,000 times that of String. Of course, this data is not very accurate. Because the number of cycles is 100000, the difference is even greater. Try it if you don't believe me.

According to the above:

Str += "here";

In fact, the processing step is to create a StringBuffer for Hou to call append (). Finally,

Then string buffer to sting ();

So str += "here"; Can be equated with

StringBuffer? sb? =? New? string buffer(str);

Someone added ("here");

str? =? sb . tostring(); Therefore, the above code directly connecting strings with "+"can be basically equivalent to the following code.

String? tempstr? =? ”abcdefghijklmnopqrstuvwxyz”;

int? Times? =? 5000;

Dragon? lstart2? =? system . current time millis();

String? str? =? "";

For what? (int? Me? =? 0; ? Me? & lt? Times; ? i++)? {

StringBuffer? sb? =? New? string buffer(str);

sb . append(tempstr);

str? =? sb . tostring();

}

Dragon? lend2? =? system . current time millis();

Dragon? Time 2? =? (lend2? -? lstart 2);

system . out . println(time 2); The average execution time is about 1563.

Therefore, if the connection string needs to be modified frequently in the program, the performance will be higher if StringBuffer is used.

=================

The difference between String and StringBuffer in C#

Simply put, it is the relationship between a variable and a constant. The contents of the StringBuffer object can be modified; Once a string object is generated, it cannot be modified, and redistribution is actually two objects.

The internal implementation of StringBuffer is different from String. StringBuffer will not generate new objects when processing strings, and it is better than String class in memory usage. So in practical use, if you often need to modify a string, such as insertion and deletion, then it is more appropriate to use StringBuffer.

String: There is no way to change the characters in an existing String in the String class. Because a single character in a Java string cannot be changed, an object called a string class in a JDK document cannot be changed. However, immutable strings have a great advantage: the compiler can set the string to * * *. ?

StringBuffer:StringBuffer class belongs to the auxiliary class, which can pre-allocate memory blocks with specified length to establish a StringBuffer area. In this way, it is much more efficient to append characters using the append method of the StringBuffer class than to append characters to an existing String using the+operator. Because every time the+operator is used to add characters to a string, the string object needs to find a new memory space to accommodate a larger string, which is a very time-consuming operation. Adding multiple characters means reallocating memory for strings over and over again. Using the StringBuffer class can avoid this problem.

StringBuffer is thread-safe and can be easily used in multithreaded programs, but the execution efficiency of the programs is relatively slow.

Common methods of StringBuffer

The methods in StringBuffer class should focus on String changes, such as adding, inserting and deleting, which is also the main difference between StringBuffer and String class.

1, add method

Public stringbuffer District Addition (Boolean B)

The function of this method is to append the content to the end of the current StringBuffer object, similar to string concatenation. After calling this method, the contents of the StringBuffer object will also change, for example:

string buffer sb = new string buffer(" ABC ");

Sb.append (true);

The value of the object sb becomes "abctrue".

Using this method to connect strings will save more content than strings, for example, it is applied to the connection of database SQL statements, such as:

StringBuffer? sb? =? New? string buffer();

String? User? =? "test";

String? pwd? =? " 123";

Sb.append ("Choose? *? From where? userInfo? Where is it? User name = ")

. Append (user)

. Add ("? And then what? pwd= ")

. Add (pwd); So the value of the object sb is the string "select * from userinfo where username = test, pwd = 123".

2. Method of deleting characters

Public string buffer delete charat (int index)

The function of this method is to delete the characters at the specified position, and then compose the remaining contents into a new string. For example:

string buffer sb = new string buffer(" Test ");

Someone (short for someone) Delete Charat (1);

The function of this code is to delete the character with the index value of 1 in the string object sb, that is, to delete the second character, and the rest will form a new string. The value of the object sb becomes "Tst".

There is another deletion method with similar functions:

Delete public stringbuffer district (int start, int end)

The function of this method is to delete all characters in the specified interval, including the interval with the starting index value and the interval without the ending index value. For example:

string buffer sb = new string buffer(" test string ");

Someone (short for someone) is deleted (1, 4);

The function of this code is to delete all characters from index value 1 (inclusive) to index value 4 (exclusive), and the remaining characters form a new string. The value of the object sb is "TString".

3. Insertion method

Common StringBuffer insert(int offset, String s s)

The function of this method is to insert the content into the StringBuffer object, and then form a new string. For example:

string buffer sb = new string buffer(" test string ");

Sb.insert(4, "false");

The function of this sample code is to insert the string false at the index value 4 of the object sb to form a new string, and then the value of the object sb is "TestfalseString" after execution.

4. Reverse method

Public stringbuffer inversion ()

The function of this method is to reverse the contents of the StringBuffer object and then form a new string. For example:

string buffer sb = new string buffer(" ABC ");

sb . revert();

After inversion, the content in the object sb will become "cba".

5.setCharAt method

public void setCharAt(int index,char ch)

The function of this method is to change the character whose index value is the index position in the object into a new character ch. For example:

string buffer sb = new string buffer(" ABC ");

sb.setCharAt( 1,' D ');

The value of the object sb becomes "aDc".

6.trimToSize method

Public void trimToSize ()

The function of this method is to reduce the storage space in the StringBuffer object to the same length as the string length, thus reducing the waste of space.

7, the construction method:

string buffer s0 = new string buffer(); A character buffer with a length of 16 bytes was allocated.

string buffer s 1 = new string buffer(5 12); A character buffer of 5 12 bytes was allocated.

8. Get the length of the string: length ()

string buffer s = new string buffer(" www ");

int I = s . length();

Meter (short for meter) returns a part of a string value.

Substring(int start) // Returns the string after the start subscript.

Substring(int start, int end) // Returns the string from the beginning to the end-1.

9. Replace the string

replace(int start,int end,String String)

s.replace(0, 1," qqq ");

10. Convert to an invariant string: toString ().