This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I mean two object may have same hash code but not returning true with equals() method .
Not only is it right, it is impossible for it to be wrong. That is, if I created Integer.MAX_VALUE + 1 unique (equals returns false) instances your class, I am guaranteed a hash code collision.
In practice, it is quite simple to create a hash code collision. I wrote a small application once to demonstrate that generating random 5 character Strings results in a collision about 3 times per second on my development machine. I wrote it because some twit was trying to use a String as a switch argument and claimed that "it works" if you call hashCode. This is despite the fact that the switch/case construct is evil in itself. [ January 27, 2005: Message edited by: Tony Morris ]