File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need Help in hashcodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need Help in hashcodes" Watch "Need Help in hashcodes" New topic

Need Help in hashcodes

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
Can anyone explain the hashCode and equals contract
How to choose hashcode values.
How the integer literals will be identified in java.

Henry Wong

Joined: Sep 28, 2004
Posts: 20535

In a nutshell, hashing is a very quick mechanism to distribute objects. It is important because when you have many object that needs to be added, removed, searched, in a collection -- hashing gives a way of doing it without constantly comparing it with other objects in the collection.

The purpose of the hashcode is to find a location -- a bucket to place object. Objects that are in different buckets, are by definition, different objects. However, objects that are in the same bucket, doesn't mean they are equal. These must be further checked with the equals() method.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amit k prabhat

Joined: Aug 09, 2008
Posts: 6
hi shekar,

For further clarification check the following thread
how many string objects will be created
I agree. Here's the link:
subject: Need Help in hashcodes
It's not a secret anymore!