aspose file tools*
The moose likes Beginning Java and the fly likes Hashcode and its importance in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashcode and its importance in java" Watch "Hashcode and its importance in java" New topic
Author

Hashcode and its importance in java

Balaguru Gupta
Greenhorn

Joined: Mar 03, 2013
Posts: 10
Can anyone explain what is Hashcode and why it is important?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14143
    
  18

Wikipedia has an article on it: Java hashCode()


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Amol Bhople
Greenhorn

Joined: Feb 28, 2013
Posts: 1
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

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.")
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashcode and its importance in java