1. = =
= = In Java is the address that compares two objects in the JVM. Better understand. Look at the following code:
public class comaddr{public static void Main (string[] args) throws Exception {String S1 = "Nihao"; String s2 = "Nihao"; String s3 = new String ("Nihao"); System.out.println (S1 = = s2); True System.out.println (S1 = = S3); False}}
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,
Public boolean equals (Object anobject) { if (This == anobject) { return true; } if (anobject instanceof string) { String anotherString = (String) anObject; int n = value.length; if (N == anotherstring.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
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
public int hashcode () { int h Private variables in the = hash; //default to 0 ### string class, if (h == 0 && value.length > 0) { Array of string contents saved in the //private final char value[]; ### sting class char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h;}
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:
Public class comhashcode{ public static void main (String[] args) throws Exception { ComHashcode A = new comhashcode (); comhashcode b = new comhashcode (); system.out.println (A.hashCode ()); //870919696 system.out.println ( B.hashcode ()); //298792720 long num1 = new long (8); long num2 = new long (8); System.out.println (Num1.hashcode ()); //8 &NBSP;SYSTEM.OUT.PRINTLN (num2.hashCode ()); //8 }}
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) The reason for the binding . Hashtable implements a hash table, in order to successfully store and retrieve objects in the Hashtable, the object used as the key must implement the Hashcode method and the Equals method. Same as (1), the object must be guaranteed equal to equals, and the hashcode is 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 = =
Reference article:
Http://www.cnblogs.com/xudong-bupt/p/3960177.html
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1688544
java = =, Equals (), hashcode () Source Code Analysis