Current location - Health Preservation Learning Network - Slimming men and women - Why is String in JAVA more efficient than StringBuffer?
Why is String in JAVA more efficient than StringBuffer?
You can see the difference between it and String, which is mainly used for splicing. The string is a variable-length string.

1, StringBuffer object initialization

Unlike the initialization of String class, the initialization of StringBuffer object has a special syntax provided by Java, but it is usually initialized by a constructor.

For example:

string buffer s = new string buffer();

The StringBuffer object initialized in this way is an empty object.

If you need to create a StringBuffer object with content, you can use:

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

The content of the StringBuffer object initialized in this way is the string "abc".

It should be noted that StringBuffer and String belong to different types and cannot be directly cast. The following codes are all wrong:

StringBuffer s = "abc// Assignment types do not match.

string buffer s =(string buffer)" ABC "; //There is no inheritance relationship, and forced transfer cannot be performed.

The code for mutual conversion between StringBuffer object and String object is as follows:

String s = " abc

string buffer sb 1 = new string buffer(" 123 ");

StringBuffer sb2 = new stringbuffer; //the string is converted to StringBuffer.

string s 1 = sb 1 . tostring(); //StringBuffer is converted into a string.

2. Common methods of 2.StringBuffer

The methods in the StringBuffer class are mainly aimed at the changes of strings, such as adding, inserting and deleting, which is also the main difference between StringBuffer and String class.

A. Additional methods

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:

string buffer sb = new string buffer();

String user = " test

string pwd = " 123 ";

sb . append(" select * from userInfo where username = ")

. Append (user)

. append(" and pwd= ")

. Add (pwd);

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

B. 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".

C. insertion method

Common StringBuffer insert(int offset, boolean b)

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 a false value 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.

D. 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".

E, 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".

F.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.