Java =, equals (), hashCode () Source Code Analysis

Source: Internet
Author: User

Java =, equals (), hashCode () Source Code Analysis
Comparison of = and equals () is often encountered in java programming or interviews. I looked at the source code and summarized it with the actual programming. 1. = in java, the = is to compare the addresses of two objects in JVM. Better understanding. See the following code: Copy code 1 public class ComAddr {2 public static void main (String [] args) throws Exception {3 String s1 = "nihao "; 4 String s2 = "nihao"; 5 String s3 = new String ("nihao"); 6 System. out. println (s1 = s2); // true7 System. out. println (s1 = s3); // false8} 9} copy the code in the above Code: (1) s1 = s2 is true, because both s1 and s2 are references of the string literal "nihao" and point to the same address, so they are equal. (2) s1 = s3 is false because the new object is generated in the heap. s3 is a reference to the variable in the heap, instead, s1 is a reference pointing to the string literal value "nihao". Different addresses are not equal. 2. equals () equals is the method in the root class Obeject. The source code is as follows: public boolean equals (Object obj) {return (this = obj);} the default equals method is displayed. You can directly call = to compare the Object address. Different subclasses can override this method to determine the equals of two objects. The following equals method is rewritten in the source code of the String class. Copy the Code 1 public boolean equals (Object anObject) {2 if (this = anObject) {3 return true; 4} 5 if (anObject instanceof String) {6 String anotherString = (String) anObject; 7 int n = value. length; 8 if (n = anotherString. value. length) {9 char v1 [] = value; 10 char v2 [] = anotherString. value; 11 int I = 0; 12 while (n --! = 0) {13 if (v1 [I]! = V2 [I]) 14 return false; 15 I ++; 16} 17 return true; 18} 19} 20 return false; 21} copy the code as shown in the preceding code, (1) equals in the String class first compares the address. If it is referenced by the same object, it can be seen that the object is equal, and true is returned. (2) If it is not the same object, the equals method compares the characters in two string objects one by one. true is returned only when they are completely equal. Otherwise, false is returned. 3. hashcode () equals is a method in the root class Obeject. By default, hashCode () in an Object returns the 32-bit jvm memory address of the Object. That is to say, if the object does not override this method, the 32 of the corresponding object is returned as the JVM memory address. The hashCode repeated in the source code of the String class is as follows. Copy code 1 public int hashCode () {2 int h = hash; // Default to 0 ### private variable in the String class, 3 if (h = 0 & value. length> 0) {// private final char value []; ### array of string content stored in the Sting Class 4 char val [] = value; 5 6 for (int I = 0; I <value. length; I ++) {7 h = 31 * h + val [I]; 8} 9 hash = h; 10} 11 return h; 12} copy the code String source code using private final char value []; Save the String content, so the String is unchangeable. In the following example, the class without rewriting the hashCode method directly returns the address of a 32-bit object in the JVM. The Long class overwrites the hashCode method and returns the calculated hashCode value: copy code 1 public class ComHashcode {2 public static void main (String [] args) throws Exception {3 ComHashcode a = new ComHashcode (); 4 ComHashcode B = new ComHashcode (); 5 System. out. println (. hashCode (); // 870919696 6 System. out. println (B. hashCode (); // 298792720 7 8 Long num1 = new Long (8); 9 Long num2 = new Long (8); 10 System. out. println (num1.hashCode (); // 811 System. out. println (num2.hashCode (); // 812} 13} copy the code Summary: (1) bind. When the equals method is overwritten, it is usually necessary to override the hashCode method to maintain the conventional protocol of the hashCode method. This Protocol declares that equal objects must have equal hash codes. (2) reason for binding. Hashtable implements a hash table. To successfully store and retrieve objects in the hash table, the objects used as keys must implement the hashCode method and equals method. Same as (1), must ensure that the equals object is equal, hashCode is also equal. Because the hash table uses hashCode to retrieve objects. (3) default. = Compare the address of the object in JVM by default. By default, hashCode returns the storage address of the object in JVM. Equal compares objects. By default, it compares the addresses of objects in the JVM.

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.