*
The moose likes Beginning Java and the fly likes Regarding Usage of Hashcode method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding Usage of Hashcode method in Java" Watch "Regarding Usage of Hashcode method in Java" New topic
Author

Regarding Usage of Hashcode method in Java

Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Read Chapter 3 of Effective Java.


[My Blog]
All roads lead to JavaRanch
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

There's a link in that page, titled "Chapter 3, Methods Common to All Objects". Click it.
Or click here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Is that the old edition? There have been slight changes, but the principles are the same as before.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
That link no longer works, unfortunately.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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.
 
Consider Paul's rocket mass heater.
 
subject: Regarding Usage of Hashcode method in Java
 
Similar Threads
hashCode() Method
Regarding hashcode method for String order
Bi-directional many-to-many mapping question
Servlet Book..
Help on Hashcode