Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hashCode( )

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following output:

JOHN DOE : Java
Calculated hash=4 for JOHN DOE
students[hash]: JOHN DOE : Java
students hashCode value = 21990827

Could someone explain why I got the hashCode value = 21990827 and NOT 4? (4 is what I was expecting?



Do I need to have a toString( ) method? Similar to when I wish to have something more meaningful when printing out the contents of an array?

Thanking you in advance!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override hashCode() in Student. If you wanted the hashCode() for a Student object to be the same as the hashCode() for the Student's name, then you could write

 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!