1. Use "= =" to compare two variables, if two variables are basic type variables, and are all numeric classes, the value is equal to return true
Returns true if two variables are reference variables, and the address of two objects is the same as that of the same object.
The 2.equals:string class overrides equals: 1) If the same object returns true;
2) if not, compare their values with the same value and return True
Rewrite code:
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; }
3. Example:
Public classString1 { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsString str1=NewString ("Hello"); String str2=NewString ("Hello"); System.out.println ("= = Comparison:" + (str1 = = str2));//falseSystem.out.println ("Equal comparison:" +str1.equals (str2)); String STR3=NewString ("str1"); String STR4= "STR1"; intHashCode1 =System.identityhashcode (STR2); System.out.println ("STR2 's address:" +hashCode1); STR2= "STR1"; System.out.println ("= = Comparison:" + (STR3 = = STR4));//falseSystem.out.println ("= = Comparison:" + (str2 = =str3)); System.out.println ("= = Comparison:" + (str2 = = STR4));//trueSystem.out.println ("Equal comparison:" + str3.equals (STR4));//true intHashCode2 =System.identityhashcode (STR2); System.out.println ("STR2 's address:" +hashCode2); HashCode2=System.identityhashcode (STR3); System.out.println ("STR3 's address:" +hashCode2); }}
Operation Result:
= = Comparison: false
Equal comparison: True
Address of str2:1704856573
= = Comparison: false
= = Comparison: false
= = Comparison: true
Equal comparison: True
Address of str2:705927765
Address of STR3:366712642
4. correct rewrite equals five points to note again:
1 reflexivity: The return value of X,x.equals (x) for any reference value must be true.
2 symmetry: For any reference value x, Y, the return value of X.equals (Y) must be true if and only if Y.equals (x) returns a value of true;
3 transitivity: If X.equals (y) =true, y.equals (z) =true, then X.equals (z) =true
4 Consistency: If the object participating in the comparison does not change, then the result of the object comparison should not have any change
5 non-nullability: any non-null reference value x,x.equals (NULL) The return value must be false
After rewriting, there is an essential difference from = =:
Equal: is used to compare whether the contents of two objects are equal, since all classes are inherited from the Java.lang.Object class, so if the method is not overwritten, the call
is still a method in the object class, and the equal method in object returns the judgment of = =, so if you do not overwrite the method, the method is called without
Any sense of it. In Java object-oriented processing, we generally choose to override the Equals method in JavaBean, and after using hibernate, we want to build the mapping file of the database and the entity
Class, which is the best way to override the Equals method in an entity class, we can implement the method according to our own definition if we follow the five principles, for example, for a student class
We define that the two objects are equal as long as the learning number is the same, and we also rewrite the Hashcode method.
= =: is used to determine whether the address of two objects is the same, that is, whether it refers to the same object. The comparison is in the real sense of the pointer operation.
Reference Blog: http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html
The difference between "= =" and the Equals method in Java