• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

hashCode() method

Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell me when hashCode() method gets invoked and
When do we need to override it?

And what value is returned for custom object in SOP, if we dont overrride
toString() method in that particular class. Its something like name of Object followed by a number. What does tht mean.
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Shaily S,"

Welcome to JavaRanch!

First, please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and last name. The first name can be an initial, but the last name cannont. You can edit your display name here.

In general, hashCode() is called whenever an object is added to or retrieved from a container using hashing (for example, a HashSet or HashMap). So if instances of your class have the potential to be stored in such a container, you should override hashCode (and equals).

hashcode() is also invoked in the default bahavior of toString(), which is described in the API documentation for java.lang.Object...

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic