java = =, Equals (), hashcode () Source Code Analysis

Source: Internet
Author: User

In Java programming or interview often encounter = =, equals () comparison. I looked at the source code, the actual programming summary.

1. = =

= = In Java is the address that compares two objects in the JVM. Better understand. Look at the following code:

1 public class Comaddr{2 public     static void Main (string[] args) throws Exception {3         String s1 = "Nihao"; 4         Stri ng s2 = "Nihao"; 5         string s3 = new String ("Nihao"); 6         System.out.println (S1 = s2);    true7         System.out.println (S1 = = S3);    False8     }9}

In the code above:

(1) S1 = = S2 is true because both S1 and S2 are references to the string literal "Nihao", pointing to the same piece of address, so they are equal.

(2) S1 = = S3 is false because the object generated by new is in the heap, S3 is a reference to the variables in the heap, but S1 is a reference to the string literal "Nihao", and the address is different so it is not equal.

2.equals ()

Equals is a 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 visible, calling = = directly, comparing the object address.

Different subclasses can override this method to make a judgment on the equals of two objects.

The Equals method overridden in the string class source code is as follows,

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 = = Anotherst Ring.value.length) {9                 char v1[] = value;10                 char v2[] = anotherstring.value;11                 int i = 0;12 while                 (n--! = 0) {                     v1[i]! = V2[i])                             return false;15                     i++;16                 }17                 return true;18             }19         }20         return false;21     }

As you can see from the code above,

(1) equals in the String class first compares the address, and if it is a reference to the same object, the object is equal and returns true.

(2) If it is not the same object, the Equals method will compare the characters within the two string object, and return true only if it is exactly equal, otherwise false.

3.hashcode ()

  Hashcode is a method in the root class Obeject.

By default, Hashcode () in object returns the 32-bit JVM memory addresses of the objects. That is, if the object does not override the method, 32 of the corresponding object is returned as the JVM memory address.

The Hashcode method that is overridden in the string class source code is as follows

1 public int hashcode () {2     int h = hash;    Default to 0 # # # of private variables in string class, 3     if (h = = 0 && value.length > 0) {    //private final char value[]; # # # An array of string contents saved in the Sting class 4         char val[] = value, 5  6 for         (int i = 0; i < value.length; i++) {7             h = + * H + Val[i]; 8         } 9         hash = h;10     }11     return h;12}

The string is immutable because it uses private final char value[];

Look at the following example, without overriding the class of the Hashcode method, directly returning the address of the 32-bit object in the JVM; the long class overrides the Hashcode method and returns the computed Hashcode value:

1 public class comhashcode{2 public     static void Main (string[] args) throws Exception {3         Comhashcode a = new Comh Ashcode (); 4         Comhashcode B = new Comhashcode (); 5         System.out.println (A.hashcode ());    870919696 6         System.out.println (B.hashcode ());    298792720 7          8         Long num1 = new Long (8), 9         long num2 = new Long (8),         System.out.println (Num1.hashcode ( ));    811         System.out.println (Num2.hashcode ());    812     }13}

Summarize:

(1) binding. when the Equals method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares that the equality object must have an equal hash code.

(2) reason for binding. Hashtable implements a hash table, in order to successfully store and retrieve objects in the Hashtable, objects used as keys must implement hashCode methods and equals methods. Same as (1), the object must be guaranteed equal to equals, hashCode also equal. Because the hash table retrieves the object through Hashcode.

(3) Default.

= = Compares the address of the object in the JVM by default.

  hashcode Default returns the storage address of the object in the JVM.

Equal compare objects, the default is to compare objects in the JVM address, with = =

java = =, Equals (), hashcode () Source Code Analysis

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.