1. Many of the online statements are: = = is the comparison content is the same, equals is the comparison is the same object. This argument is wrong.
Note: There are two key points to keep in mind when testing this problem with a variable of type string:
The 1.String type overrides the Equals method, comparing whether the content is equal.
The 2.String type is the final type:
String str1 = "abc"; String str2 = "abc"; STR1 and str2 point to the same object.
String str3 = new String ("abc"); String STR4 = new String ("abc"); STR3 and Str4 point to two different objects.
2. So, the right thing is:
for = = , it is not an absolute comparison of whether the content is equal. There are two cases: if you are comparing the basic data type of java, = = Compare the content is equal;
If the reference type is compared, the comparison is whether the same object.
for equals , equals is not allowed to compare the base data type, only the reference type is compared, and the comparison is the same object unless the Equals method is overridden.
However, there are some types that override the Equals method and compare whether the content is equal. For example, types such as String,date override the Equals method.
*********************************************
Verify:
Output Result:
Explain:
The number 1:string class overrides the Equals method and compares the contents, so the output is true.
Number 2:string is a reference type, = = is compared here to whether it is the same object, the output is true. So prove that str1 and str2 are the same object, for what?
At the beginning, string is the final type, is an immutable object, and if the contents of the two string are the same, the second new string is created if the object is not created by the new method
, instead of re-creating a new string, reference the same string. Because the contents of str2 and str1 are the same, str2 points to the STR1 string instead of creating a new string.
The number 3:string class overrides the Equals method and compares the contents, so the output is true.
Number 4:string is a reference type, = = is compared here to whether the same object is pointed to. STR4 is a new object through new, so STR3 and Str4 point to different objects, and the output is false.
The numbers 5:STR3 and STR4 are all new objects through new, so they are different objects and the output is false.
The number 6:string class overrides the Equals method and compares the contents, so the output is true.
The difference between the equals of Java and the "= ="