This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What's wrong with my hashCode() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with my hashCode() ?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662



I am calling different instances, however, it returns the same hashCode() for different instances:


returns
172 [main] DEBUG poker.Poker - 217
172 [main] DEBUG poker.Poker - 217
172 [main] DEBUG poker.Poker - 217

Not sure why they are being returned in triplicate either.



returns
172 [main] DEBUG poker.Poker - 217
...(x3)








Jon
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20133
    
  46



It would help if you print out more details -- such as "name" or other parts of the instance, in addition to the hash code. We don't know anything about the instances that you are referring to.

And BTW, it is valid for two unequal instances to have the same hash code -- so there may be nothing wrong.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

Henry Wong wrote:

It would help if you print out more details -- such as "name" or other parts of the instance, in addition to the hash code. We don't know anything about the instances that you are referring to.

And BTW, it is valid for two unequal instances to have the same hash code -- so there may be nothing wrong.

Henry


I think I resolved it now:

Output
847 for _name = "Johnny"
847 for _name = "Johnny"
729 for "_name = "Peter"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44433
    
  33
Why do you start your variable names with underscores?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong with my hashCode() ?
 
It's not a secret anymore!