String is always an important part of C ++ or Java. In actual development, string-related content is also frequently used.
About "=" and equal and hashcode
Before that, we need to add some knowledge about the object class:
The Java. lang Package is automatically imported by the system, and the object class is in the lang package.
For native data types, "=" means to compare whether the numbers represented by the two data types are equal.
For the reference type, "=" indicates whether the reference on the left and the reference on the right point to the same object, even if only the address is compared, not the content pointed, that is to say, it only compares whether the reference addresses on both sides are the same.
To print a reference type directly, the tostring method is automatically called by default. In the definition of the object type, the tostring method prints the following content: getclass (). getname () + '@' + integer. tohexstring (hashcode () is the hexadecimal representation of the printed object class name and the address to which this object is referenced. In the string class, the tostring method is overwritten, And the content indicated by the string is printed directly. The hashcode method is also rewritten, the hashcode value is not related to the memory address pointed to by the reference, but to the actual content of the string instance. For different string type references, if the same content exists, then their hashcode is the same.
To compare the content of two strings, you must use the equal method. Note that the equals method is overwritten in the srring class. In the object class, the equals method is equivalent to "=", that is, to compare whether the referenced address is the same:
Public Boolean equals (Object OBJ ){
Return (this = OBJ );
}
In the string method, pay special attention to the source code. It is mainly used for reference in this mode and requires integrity and fault tolerance, such as the start of two steps. In many cases, the equals method needs to be rewritten, which is easier to measure during the written test. The equals method of the string class is as follows:
Public Boolean equals (Object anobject ){
// If the referenced address is the same as the address, true is returned and no comparison is made.
If (this = anobject ){
Return true;
}
// Because the parameter is of the object type. If the parameter is of the string type, false is returned based on the comparison character.
If (anobject instanceof string ){
String anotherstring = (string) anobject;
Int n = value. length;
If (n = anotherstring. value. Length ){
Char V1 [] = value;
Char V2 [] = anotherstring. value;
Int I = 0;
While (n --! = 0 ){
If (V1 [I]! = V2 [I])
Return false;
I ++;
}
Return true;
}
}
Return false;
}
// Generally, this type of boolean type can also set a return value by default. For example, when the function starts, let B = false in the middle, adjust B according to the actual situation, and return B at the end.
If you want to explain the equals method during the interview, you must separately explain what the object class is like and what the rewriting is like if it is not rewritten for other classes.
About string pool
Two string Assignment Methods:
String S = "hello" is called the literal value assignment method.
String S = new string ("hello"). This creates a String object using the new method.
Because the string class is frequently used in actual development, it is necessary to maintain a string pool. The string pool is a space in the stack memory and mainly has the following functions:
Generate a String object by literally assigning values, such as string S = "hello ":
1. check from the string pool to see if there is a hello object,
2. If no, create a hello object in the string pool and return the address in the string pool.
3. If a hello object exists in the string pool, the address of the hello object is returned.
Generate a String object using the new method, such as string S = new string ("hello ")
1. First, check whether there is a hello object in the string pool.
2. If a hello object exists in the string pool, create a hello object in the heap and return the address in the heap.
3. If no hello object exists in the string pool, create a hello object in the string pool, create a hello object in the heap, and return the address in the heap.
The above basic knowledge is clearly understood, and it is easier to solve this common string comparison problem:
Public class stringtest {
Public static void main (string [] ARGs ){
String str1 = "hello"; // str1 points to the address in the string pool
String str2 = new string ("hello"); // str2 points to the address in the heap.
String str3 = new string ("hello"); // str3 points to the address in the heap.
Object str4 = new string ("welcome ");
System. Out. println (str1 = str2 );
// False str1 indicates the address in the stack. str2 indicates the address in the stack.
System. Out. println (str2 = str3 );
// False str2 is the heap address. str3 is another address in the heap pointing to different addresses (not the same object) but containing the same content.
System. Out. println (str1 = "he" + "llo ");
// True indicates the address in the string pool.
System. Out. println (str1 = "El" + "Lo ");
// True Same as above
System. Out. println (str2 = "El" + "Lo ");
// False str2 is the address in the heap.
}
}
For the intern method of a string, a standard representation of the corresponding string is returned. For example, "ABC" is called ". intern (). If "ABC" exists in the string pool, the return address is returned. If not, the "ABC" exists in the string pool and the return address is in the string pool.
About stringbuffer
The string type is a constant, which cannot be changed after being defined. Every time two strings are concatenated in the form of "+", a new string is actually generated. In Java, you can use stringbuffer to modify strings. stringbuffer is a variable and can dynamically append new content to it. The tostring method can be used to convert it into a string. Stringbuffer buffer = new stringbuffer ("XXX"); buffer. append (XXX ). apend (XXX) Here, each append operation does not return a new reference, but it is still the original reference. Although there are multiple append operations, there is always only one stringbuffer object.
Some basic knowledge about the string class