Pre-Analysis Basics understand:
A) The JVM divides the memory into two types: one is stack memory, and the other is heap memory.
① variables and reference variables (variable names) for some of the basic types defined in the function are allocated in the function's stack memory.
② when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for that variable, which can be used immediately by another.
③ heap memory is used to hold objects created by new (including classes wrapped by a basic type: Integer, String, Double, in fact each base type has his wrapper class) and arrays.
ii) The Equals method in the object class:
public boolean equals (Object obj) {
return (this = = obj);
}//You can see that the Equals method in the object class uses = = To determine whether the object reference points to the same memory address.
c) The string class is special, and the string class overrides the Equals method, as follows:
public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
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;
}//You can see that the Equals method in the string class can not only determine if the object's memory address is equal, but also returns true. If the preceding judgment is not true, then determine if the object in parentheses is of type string, and then determine whether the length of the two string objects is equal, the final judge is equal, and true if equal.
IV) Start specific analysis:
4.1 Comparison in the base data type
int NUM1 = 10;
int num2 = 10;
System.out.println (NUM1 = = num2); True
NUM1 and num2 are both int types and their values are 10, so using "= =" is certainly equal.
4.2 Comparisons in reference data types
⑴string S1 = "Hello";
String s2 = "Hello";
System.out.println (S1 = = s2); True
System.out.println (s1.equals (S2)); True
! [] (http://i2.51cto.com/images/blog/201809/01/c00e1eaaca2f6f2fe06c9643d522c1ef.png?x-oss-process=image/ watermark,size_16,text_qduxq1rp5y2a5a6i,color_ffffff,t_100,g_se,x_10,y_10,shadow_90,type_zmfuz3pozw5nagvpdgk=) When executing string s1 = "Hello"; this statement finds the string "chance" in the character constant pool in the heap, and if not found, puts the string "Hello" into the string constant pool. In the stack, a space called S1 is stored "chance", A reference to this space. When executing string s2 = "Hello"; this statement will find the string "chance" in the string constant pool in the heap, and it is obvious that it can be found, so it assigns the reference address of the string "chance" in the character constant pool to S2, So s1 and S2 are all stored in a reference to the same "chance" in the heap's character constant pool, followed by System.out.println (s1.equals (S2)), where equals is rewritten in the string class. Used to compare the actual contents of two strings for equality, that is, if each character is equal, the end of the overridden method will be explained in other words!!! Because the comparison is the string content, the S1,S2 content is chance of course is equal. (2) String SS1 = "AAA"; String ss2 = new String ("AAA"); System.out.println (SS1 = = SS2); False System.out.println (Ss1.equals (SS2)); True! [] (http://i2.51cto.com/images/blog/201809/01/b5d497d07116d56b43dd1d597d5f65b9.png?x-oss-process=image/ Watermark,size_16,text_qduxq1rp5y2a5a6i,color_ffffff,t_100,g_se,x_10,y_10,shadow_90,type_zmfuz3pozw5nagvpdgk=) SS2 is new, so the memory address is re-allocated, and when determined with = =, returns false, but the contents of two strings are the same, so when using the Equals method, it returns true.
(3) String sss1 = new String ("AAA");
String sss2 = new String ("AAA");
System.out.println (Sss1 = = Sss2); False
System.out.println (Sss1.equals (SSS2)); True
 sss1和sss2都为new出来的对象,各占有一块内存空间,所以内存地址不同,但是字符串内容相同。
(4) Non-string classes, such as the StringBuffer class, do not override the Equals method, so the content is not compared. = = and equals are the memory addresses of the comparison
StringBuffer StringBuffer = new StringBuffer ("AAA");
StringBuffer stringBuffer2 = new StringBuffer ("AAA");
System.out.println (StringBuffer = = StringBuffer2); False
System.out.println (Stringbuffer.equals (stringBuffer2)); False
Explanation of the difference between = = and equals in Java