Java-parsing equals from the stack constant pool
I. basic concepts ① basic data types in JAVA (simple type, built-in type): byte type (byte), short INTEGER (short), INTEGER (int), long integer (long ), character type (char), float type (float), double type (double), boolean Type (boolean); note that all data types are lower case, no String type, and String type is not the basic data type in Java. ② Variable types (differentiated by the scope of variables): global variables, member variables, and local variables. ③ Heap: stores all new objects. The object references exist in the stack. When the references in the stack disappear, the garbage collector recycles the objects in the heap. ④ Stack: stores the reference of basic types of variables and the reference of other objects, but the object itself is not stored in the stack, but stored in the heap (new object) or constant pool (String constant objects are stored in the constant pool .) The data size and lifecycle in the stack can be determined. When no reference points to the data, the data will disappear. ⑤ Constant pool: stores string constants and basic type constants (final modified data that can be identified during compilation and saved in the compiled. class file ). 1 package cn.edu. whu. compare; 2 3 public class CompareTest {4 public static void main (String [] args) {5 // "AB" has a constant pool, a points to the address of "AB" in the constant pool. 6 String a = "AB"; 7 // "B" exists in the constant pool. 8 final String bb = "B "; 9 String cc = "B"; 10 String B = "a" + bb; 11 String c = "a" + cc; 12/** 13 * After compiling the value of variable B, you can determine the address pointing to the "AB" in the constant pool, because bb uses final modification and bb points to the determined String 14 * the value of variable c can only be determined at runtime. Variable c is equivalent to a new String object at runtime, this new object is stored in the heap 15 */16 System. out. pr Intln (bb = cc); // true17 System. out. println (a = B ); // true18/** 19 * a points to the address of "AB" in the constant pool. c points to the address of the object "AB" created at runtime in the heap. 20 * c. intern () returns a String object. This String object points to ---> c. The address of the object "AB" in the constant pool is 21 */22 System. out. println (a = c); // false23 System. out. println (a = c. intern (); // true24} 25} II. differences between equals () and = ① about = Operator this operator compares the values of two objects (variable names and constant names in the previous instance) in the stack (a memory address, this address stores the actual Object content). If the value is equal, True is returned. Otherwise, False is returned. ② The equals method defined by the Object class is returned: 1 public boolean equals (Object obj) {2 return (this = obj); 3}: the equals of the Object class directly compares the memory addresses of two objects through =, that is to say, if any subclass inherited from the Object class does not override the equals method, the results of the comparison methods equals and = are the same. ③ Equals method 1 public boolean equals (Object anObject) defined by the String class {// if the memory addresses of the two comparison objects are the same, true is returned without comparing them, because the memory addresses are all equal, the memory must be the same !! 2 if (this = anObject) {3 return true; 4} // compare each character in a string, if (anObject instanceof String) {6 String anotherString = (String) anObject; 7 int n = value. length; 8 if (n = anotherString. value. length) {9 char v1 [] = value; 10 char v2 [] = anotherString. value; 11 int I = 0; 12 while (n --! = 0) {13 if (v1 [I]! = V2 [I]) 14 return false; 15 I ++; 16} 17 return true; 18} 19} 20 return false; 21} refer to the comment above, it is unnecessary.