java类库详细讲解

源代码在线查看: gethash.html

软件大小: 5593 K
上传用户: add505
关键词: java
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				Comparing Object Values Using Hash Codes
				(Java Developers Almanac Example)
				
				
				
				
				
				
								    BODY CODE  {font-family: Courier, Monospace;				           font-size: 11pt}				    TABLE, BODY				          {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 10pt}				    PRE   {font-family: Courier, Monospace;				           font-size: 10pt}				    H3    {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 11pt}				    A.eglink {text-decoration: none}				    A:hover.eglink {text-decoration: underline}				    -->
				
				
				
				
				
				The Java Developers Almanac 1.4
				
				        Order this book from Amazon.
				    
				
				
				
				
				
				
				
				
				
				
				
				Home
				    >
				    List of Packages
				    >
				    java.lang
				         [58 examples]
				        
				        >
				        Objects
				             [3 examples]
				            
				
				  
				    e57.  
				    Comparing Object Values Using Hash Codes
				
				The hash code of an object is an integer value that's computed using
				the value of the object. For example, for a String object, the
				characters of the string are used to compute the hash code. For an
				Integer object, the integer value is used to compute the hash code.
				
				 Hash codes are typically used as an efficient way of comparing
				the values of two objects.  For example, if the hash code of the
				string "hello" is 33, another String object with the same
				contents would also a hash code of 33.
				
				 If the hash codes of two object values are different, the
				object values are guaranteed to be different.  However, if the hash
				codes of two object values are the same, the object values are
				not guaranteed to be the same.  An additional call to
				Object.equals() must be made to confirm that the object values
				are the same.  A good hash code algorithm will minimize the chance of
				two different values having the same hash code.
				
				 The `==' operator is the most efficient way to determine if two
				objects (rather than object values) are the same.  However, in very
				limited applications, it may be necessary to get the hash code of an
				object (called the identity hash code) rather than of the object
				value.  For example, a hash table of objects requires the use of the
				identity hash code.
				
				
				
				    File file1 = new File("a");
				    File file2 = new File("a");
				    File file3 = new File("b");
				    
				    // Get the hash codes
				    int hc1 = file1.hashCode(); // 1234416
				    int hc2 = file2.hashCode(); // 1234416
				    int hc3 = file3.hashCode(); // 1234419
				    
				    // Check if two object values are the same
				    if (hc1 == hc2 && file1.equals(file2)) {
				        // They are the same
				    }
				    
				    // Get the identity hash codes
				    int ihc1 = System.identityHashCode(file1); // 1027049
				    int ihc2 = System.identityHashCode(file2); // 14642381
				    int ihc3 = System.identityHashCode(file3); // 6298545
				
				
				
				
				             Related Examples
				        
				
				
				
				
				e56. 
				    Cloning an Object
				
				
				
				e58. 
				    Wrapping a Primitive Type in a Wrapper Object
				
				
				
				
				
				
				
				
				        See also: 
				
				    Arrays
				  
				
				
				    Assertions
				  
				
				
				    Classes
				  
				
				
				    Commands
				  
				
				
				    Numbers
				  
				
				
				    Strings
				  
				
				
				    System Properties
				  
				
				
				    Threads
				  
				
				
				
				
				
				
				
				
				
				© 2002 Addison-Wesley.
				
				
				
							

相关资源