The difference between = and equals in string:
= The references in the comparison string are equal
Equals compares the same content in a string (because the string has the equals rewrite method)
Example:
/The equals and = *****************/
/*
* = The references in the comparison string are equal.
* Equals compares the same content in a string (because the string has the equals rewrite method)
*/
// Public static void main (string [] ARGs ){
// String S1 = new string ("ABC ");
// String S2 = new string ("ABC ");
// System. Out. println (s1.equals (S2); // true
// System. Out. println (S1 = S2); // false
//
// String S3 = new string ("aaaa ");
// String S4 = S3; // S3 assigns a reference value to S4
// System. Out. println (s3.equals (S4); // true
// System. Out. println (S3 = S4); // true
//}
Differences between string and stringbuffer: 1. the string class cannot be changed, while the stringbuffer class can be modified.
2. the string class overwrites the equals method of the object, while the stringbuffer does not
3. the string class can be connected using "+", while the stringbuffer object cannot be connected using "+" and the append method.
Example
/************* Use of stringbuffer *****************/
// Public static void main (string [] ARGs ){
/// The string cannot be modified.
// String STR = "ABC"; // create a String object
// STR = "AAA"; // create a new string
// STR = STR + "www"; // create a new string
//
//// Stringbuffer modifiable string
// Stringbuffer buffer = new stringbuffer ();
// System. Out. println ("Size:" + buffer. Length ());
// System. Out. println ("Capacity:" + buffer. Capacity ());
// Buffer. append ("ABC"); // append a string
// System. Out. println ("Size:" + buffer. Length ());
// System. Out. println ("Capacity:" + buffer. Capacity ());
// Buffer. append ("DDD ");
// System. Out. println ("Capacity:" + buffer. Capacity ());
// Buffer. append ("wwwwwwwwwwwwwww ");
// System. Out. println ("Capacity:" + buffer. Capacity ());
//
// Stringbuffer strbuffer1 = new stringbuffer ("www ");
// Stringbuffer strbuffer2 = new stringbuffer ("www ");
/// Stringbuffer does not overwrite the equals method, so the comparison is to determine whether the reference is equal.
// System. Out. println (strbuffer1.equals (strbuffer2 ));
//}
Stringbuilder and stringbuffer:
Stringbuilder is also an operation string class, which has the same functions as stringbuffer,
The main difference is that stringbuilder is non-thread-safe, and stringbuffer is
Thread security, so stringbuilder is highly efficient.
If the environment does not require thread security, you can use stringbuilder.