Comparison of Equals with "==" operators

zhaozj2021-02-17  73

The equals method is a method of the Object class. All classes inherited from the Object class will integrate this method, and you can overload this method to achieve their respective comparison operations, and JDK is also recommended. So developers can implement their own equals methods in their own classes to complete their own specific comparison functions, so there is no absolute relationship between equals methods and = = = =, this is based on their own implementation Look. That is to say, there are two situations where: Equals method and = = the same or different. In most cases, the difference between the two is to compare the reference to the object or compare the value of the object (other special circumstances are not considered). So == What is the operator? = = Operator is a reference to the object of the comparison rather than the value of the object. And by the following source code, it can be seen that the equals method in the original Object object is the same as = = operator completion function.

Source: java.lang.object.equals () Method: ------------------------------------- ------------------------ Public Boolean Equals (Object Obj) {Return (this = = Obj);} --------- -------------------------------------------------- - The following explanation is given in the JDK documentation: ---------------------------------------- --------------------- The Equalss Method Implements An Equivalence Relation: · IT IS Reflexive: for any reference value x, x.equalss (x) Should Return True. · IT IS SYMMES X and Y, X.Equalss (Y) Should Return True IF and Only if Y.Equalss (x) Returns True. · IT IS Transitive: for Any Reference Values ​​X, Y, And Z If X.Equalss (Y) Returns True, THEN X.EQUALSS (Z) SHOULD RETURN TRUE. · IT IS Consistent: for Any Reference Values ​​X and Y, Multiple Invocations of x.Equalss (y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified. · For any non-null reference value x, x.equalss (null) should retur . N false The equalss method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values ​​x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equals objects must have equals hash codes. --- -------------------------------------------------- ------ Conditions by the above notior know the equals method and = = operator is the same comparison function, which is compared to the reference.

So what is the Equals method of the String class we are familiar with? Let's take a look at its code and comment: Source code: ------------------------------------- ------------------------ Public Boolean Equals (Object Anobject) {i (this == Anobject) {Return True;}} {String AnotherString = (string) Anobject; int n = count; if (n == anotherstring.count) {char v1 [] = value; char v2 [] = anotherstring.value; int i = offset; int J = anotherstring. Offset; while (N-! = 0) {IF (V1 [i ]! = V2 [j ]) Return false;} return true;}} Return false;} ------------ -------------------------------------------------this Note in the method: --------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------- This Object. ------------------------------------------------- -------------- The equal method of the String class is compared to the value of the object. According to the above discussion, conclusions can be concluded: equal method and = = no difference between the operator is to be partially treated, to be specifically determined according to each implementation of Equal. *****************************

Belter (belter@sina.com)

None.blogdriver.com

转载请注明原文地址:https://www.9cbs.com/read-29704.html

New Post(0)