First, there are three classes in Java that manipulate strings:
- Character is a single character operation;
- String to manipulate a string of characters. immutable classes;
- StringBuffer also operates on a string of characters, but the variable class
String:
Is the object is not the original type.
As an immutable object, you cannot modify its value once it has been created.
The modification of a string object that already exists is to recreate a new object and then save the new value in.
A String is the final class, which cannot be inherited.
StringBuffer:
is a mutable object that does not re-establish the object as a string when modifying it
It can only be created by a constructor function,
StringBuffer sb = new StringBuffer ();
Note: You cannot pay a value by paying the value symbol.
SB = "Welcome to here!"; /error
After the object is created, the memory space is allocated in memory and a null is initially saved. to StringBuffer
The Append method can be used when paying the value.
Sb.append ("Hello");
StringBuffer is more efficient in string join operations than string:
String str = new String ("Welcome to");
str + = "Here";
The processing step is actually by building a stringbuffer, let Hou call append (), and finally
Then the StringBuffer tosting ();
In this case, the connection operation of string is more than stringbuffer some additional operations, of course, the efficiency of the compromise.
And because the string object is an immutable object, each operation sting a new object to hold the new value.
So the original object is useless, it will be garbage collection. This is also to affect performance.
The difference between Java Foundation--string and StringBuffer