This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Can someone give me examples which explain the concept of hashCode ? Specifically when comparing hashcode of two different objects, or objects of the same class, or two different objects of the same class and other possible options . Thanks
Hi,hashcode() returns the memory location of the object on which it is called.I do not want to confuse u but try this: String s="hello"; String s1=new String("hello"); If I am correct one is created in the literal pool while the other on the heap.Even then the hashcode() returns the same value for both.How I do not know.If u have any idea,tell me.
They are numerous ways to generate hash code. One of them being for Strings : first letter's hash plus the next ones and so on... You can also implement the hashcode() method for your class and generate a desired sort of hash value.
"...it's more of hiding the truth than really being faithful...oh this cruel cold world..."
Hi Angela, The hashCode implementation is up to the developer. However, it must follow some rules. Rule 1:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)
The bold part of the above code is to show you what Vedhas might have been referring to. Notice that it says that it is not a requirement to send you back a memory location at all ... As for example code the first two rules can be illustrated with the following code.
[This message has been edited by Manfred Leonhardt (edited August 22, 2001).]
Hi, Angela, hope the following code help you understand hashCode and equals:
Originally posted by Angela Narain: Can someone give me examples which explain the concept of hashCode ? Specifically when comparing hashcode of two different objects, or objects of the same class, or two different objects of the same class and other possible options . Thanks
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Joined: Apr 14, 2001
W.r.t to Rule 3, i added the following lines in the example : String s2 = new String( "Consistant" ); String s3 = new String( "Inconsistant "); ... System.out.println("\n"); System.out.println(" For two different objects " ); System.out.println( "Equals: " + s2.equals( s3 ) ); System.out.println( s2.hashCode() + " - " + s3.hashCode() + " = " + (s2.hashCode() - s3.hashCode()) );
... It gives the Output : For two different objects Equals: false 1489649582 - -1362757651 = -1442560063 As the equals method returns "false", the hashCode values are also different. I quite did not understand what i means by improving performance ?