Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Usage of Hashcode method in Java

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Could anyone please let me know the use of hashcode() method in realworld application?

I could know following things from internet about hashcode method.

1. It is the mapping of memory address of an object to an integer value.
2. Hashtable, HashMap and HashSet make use of hashcode() method.

But still my understanding about use of hashcode() method is not clear.

Could anyone please explain or let me know any reference / link available for the exact context of hashcode() method?

Thanks in Advance.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Chapter 3 of Effective Java.
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reference link.

I tried the suggested link but it takes me to the Oracle site. But i able to find the content of Chapter 3 of Effective Java.

Do i need to register to Oracle site or is there any direct link available for Chapter 3?

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a link in that page, titled "Chapter 3, Methods Common to All Objects". Click it.
Or click here.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the old edition? There have been slight changes, but the principles are the same as before.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That link no longer works, unfortunately.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is that the old edition? There have been slight changes, but the principles are the same as before.

It's the one listed in our JavaBeginnersFaq.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Shukla wrote:1. It is the mapping of memory address of an object to an integer value.

No, forget about this, it does not have anything to do with the memory address of an object, and it's not important to know this.

The API documentation of the method java.lang.Object.hashCode() says:
API documentation wrote: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 JavaTM programming language.)

But this is just an implementation detail, it is not a requirement, and the implementation might be different in reality. If you override hashCode(), you will typically not do anything with the memory address of the object; it's not even possible to get the memory address of an object in pure Java.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote: . . . It's the one listed in our JavaBeginnersFaq.
Yes, that was the old edition. It used to be possible to download a PDF of chapter 3 as a sample chapter, but they have changed the link and it now directs to the front page of an Oracle Java Developers website.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic