Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

When does hashCode gets Called !!

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

When does the hashCode method in a class gets called.

If we are putting the objects is HashCode a few methods are add , equals , contains , containsAll , toString .

And what if i put the object in HashMap when will the hashCode method of that class be called ..


Thanks in advance
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashCode() method is used by collection classes such as HashSet and HashMap. It will be called by those collection classes when you add or lookup objects. If you want to know when it's called on your object, then add for example a System.out.println(...) call in your hashCode() method:
 
Marshal
Posts: 69884
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That hashCode method is called on the "K" objects in a Map whenever it is used as a Key for "put" or "get" or it is sought with "containsKey."
I don't think it is used for "add" or "equals" which are not actually Map methods.

Go into your Java installation folder, find a file called src.zip, unzip it, go into the java folder, then util, then find a class which implements Map, eg HashMap. You can read the code and see how the hash code is used.
 
Puja Agarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you, just one last doubt ; i found it weird the hasCode method gets called even if i print the HashCode object .

I am pasting the code along with output






Please Explain ...

Regards
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you print any object , the objects toString() method gets called , which , in turn prints some meaningful information explaining it . But if that class dosent override toString() method, the object simply print its hash code value !

Correct me if i am wrong in explaining !

Thanks,
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you print a HelloEx method with System.out.println(), the toString() method is called on the HelloEx object to convert it to a string that can be printed.

Since your HelloEx class does not have its own toString() method, the toString() method of class Object is called. And the toString() of class Object returns a string of the form:

ClassName@HashCodeHex

(the name of the class, an @, and the hash code of the object in hexadecimal format). So, Object.toString() is calling your hashCode() method at that time.
 
Puja Agarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
    Bookmark Topic Watch Topic
  • New Topic