Equals and "="

Source: Internet
Author: User
Comparison between equals and the "=" Operator

--------------------------------------------------------------------------------

The equals method is a method of the object class. All classes inherited from the object class are integrated with this method, and this method can be reloaded to achieve their respective comparison operations, JDK also recommends this method. Therefore, developers can implement their own equals methods in their own classes to complete their specific comparison functions. Therefore, there is no absolute relationship between the equals methods and = of each class, this depends on the implementation of each class. That is to say, there may be two situations: The equals method and = are the same or different. In most cases, the difference between the two is whether to compare the object reference or compare the object value (this is not considered in other special cases ). So what is the = Operator compared? = The operator is the reference of a comparative object rather than the object value. The following source code shows that the equals method in the original object is the same as the = Operator to complete the function.
Source code:
Java. Lang. Object. Equals () method:
-------------------------------------------------------------
Public Boolean upload SS (Object OBJ ){
Return (this = OBJ );
}
-------------------------------------------------------------
The JDK documentation provides the following explanations:
-------------------------------------------------------------
The specified SS method implements an equivalence relation:
? It is reflexive: For any reference value X, X. Processing SS (x) shoshould return true.
? It is unsupported Ric: For any reference values X and Y, X. Duplicate SS (y) shoshould return true if and only if y. Duplicate SS (x) returns true.
? It is transitive: For any reference values X, Y, and Z, if X. restore SS (y) returns true and Y. parse SS (z) returns true, then X. writable SS (z) shoshould return true.
? It is consistent: For any reference values X and Y, multiple invocations of X. except SS (y) consistently return true or consistently return false, provided no information used in your SS comparisons on the object is modified.
? For any non-null reference value X, X. Empty SS (null) shocould return false.
The specified SS 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.
-------------------------------------------------------------
The preceding annotations show that the equals method and = Operator have completed the same comparison function and compared the object references. So what is the equals method of the string class that we are familiar with to compare? Let's take a look at its code and comments:
Source code:
-------------------------------------------------------------
Public Boolean equals (Object anobject ){
If (this = anobject ){
Return true;
}
If (anobject instanceof string ){
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;
}

-------------------------------------------------------------
Annotations for this method are:
-------------------------------------------------------------
Compares this string to the specified object. The result is true if and only if the argument is not null and is a string object that represents the same sequence of characters as this object.
-------------------------------------------------------------
The equal method of the string class can be obtained from the code and comments above to compare the object values.
Based on the above discussion, we can conclude that the equal method and the = operator must be treated individually based on each implementation of equal.
*******************************
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.