1. Similarities and Differences between string and stringbuilder
String is an unchangeable data type. Once a String object is initialized, the string object cannot be changed. Method for modifying string content
And operator are actually creating a new string.
The stringbuilder allocates memory and modifies the string in the storage unit assigned to the stringbuilder instance.
Example:
The string cannot be modified:
String STR = "";
STR = "B" // STR is assigned "B". That's right, but the original string value "A" in STR is not modified.
String STR = "";
String str1 = STR;
STR + = "B"; // STR = "AB", str1 = "";
Stringbuilder STR = new stringbuilder ("");
Stringbuilder str1 = STR;
Str. append ("B ");
Str. tostring () and str1.tostring () are both "AB ".
2. Explanation string STR = NULL/string STR = ""
STR = NULL, declared regardless of memory space, STR = "" declared and divided into memory space
3. String S = new string ("XYZ"); how many string objects are created?
Two objects: xyx and xyx.
4. Can I inherit the string class?
The string class is a final class, so it cannot be inherited.
5. String S = "";AndString S = string. empty;What is the difference? which method is better?
String S = "";Allocate a block of memory for storage first""Text constant,SReference this memory,String S = string. Empty;SReferenceStringThe static variable address in the class (the static variable will be initialized at the beginning, so no matter how many times it is referenced, only one memory will be allocated). When comparing the two, the latter has one less memory allocation, therefore, it is more efficient.