Reference: http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html
The data types in Java can be divided into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten, such as String,integer, In these classes, date equals has its own implementation, rather than the comparison class's storage address in heap memory.
For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).
As mentioned above, Java = = Compares references, that is, in-memory addresses, equals () if there is no rewrite is also a reference to the comparison, after overriding the reference, referring to different re-comparison content. The above blog is very clear, here I posted the object in Equals () and string Equals () of the source code
1 Public Boolean equals (Object obj) {2 return ( this = = obj); 3 }
String.Equals ()
Public Booleanequals (Object anobject) {if( This==anobject) { return true; } if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =value.length; if(n = =anotherString.value.length) {CharV1[] =value; CharV2[] =Anotherstring.value; inti = 0; while(n--! = 0) { if(V1[i]! =V2[i])return false; I++; } return true; } } return false; }
You can see that the Equals () in object directly compares the reference. String.Equals () compares references first, referring to different content in comparison.
Ps:java String is a constant, and its value cannot be changed. If you want to change, please use StringBuffer to provide append () and other methods. and pay attention to the STIRNG pool in Java.
The difference between equals and = = in Java