• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashcode and its importance in java

 
Balaguru Gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain what is Hashcode and why it is important?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
 
Amol Bhople
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search on net. Some good info in blogs too. sharing one link
http://manikandanmv.wordpress.com/tag/equals-and-hashcode-example-in-java/
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Amol Bhople wrote: . . . Some good info in blogs too. . . .
That blog is a poor source; it has some serious errors in its text and in its code.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain what is Hashcode and why it is important?


The "what" part of your question is a mathematical concept. When applied to Java(public int hashCode() method of Object class), it represents an integer value which is a transform of the hexadecimal memory representation of the object on the heap. To understand its importance, first you need to understand about data structures in Java, especially those which use hashing algorithms to store elements in them(like HashMap, HashSet, Hashtable etc..). Read about collection framework first. Then I would be in a better position to explain to you the "why is it important" part of your question.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:When applied to Java(public int hashCode() method of Object class), it represents an integer value which is a transform of the hexadecimal memory representation of the object on the heap.


That statement is somewhere between incorrect and meaningless.

In fact it's not specified how Object will compute its hashCode, although it's "typically implemented by converting the internal address of the object into an integer". Note that "internal address" doesn't really have any relation to "hexadecimal memory representation," whatever that latter is supposed to mean.


 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how exactly does the JVM calculate the hashCode() for an Object? How is it implemented? And what does internal address mean? It would be some bit pattern. Correct?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried looking up the code of OpenJDK but couldn’t find the native code which calculates Object#hashCode.
It is not something you need to know.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:So how exactly does the JVM calculate the hashCode() for an Object? How is it implemented?


We don't know and we don't care. It's not specified.

And what does internal address mean?


It refers to the actual memory address in the computer where the JVM sees the object as being stored. Note that because of virtual memory, that may not be the same as the physical RAM address of the object.

It would be some bit pattern. Correct?


Of course it would. What data is stored in a computer that is NOT a bit pattern? (Hint: The answer is, "None.")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic