Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Hashcode and its importance in java

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain what is Hashcode and why it is important?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wikipedia has an article on it: Java hashCode()
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Marshal
Posts: 74778
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74778
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.")
 
reply
    Bookmark Topic Watch Topic
  • New Topic