Hi everyone,
I just tried the John Meyer's
SCJP 5 Mock Exam and I am a little bit unsure about one of the questions:
One of the answers that is supposedly correct is:
If hashCode() is not overridden then regardless of whether line 1 returns true or false 2 will be printed on the screen.
Is this really correct? If line 1 is changed to return true, then all keys to the map that end up in the same hash-bucket will be considered equal. While the default hashCode() says that the hashcodes of distinct objects will be distinct "as much as is reasonably practical", there is no guarantee. E.g., if you generate new keys in a for-loop you will soon get overlap:
Running this for a few different MAX values will result in random equal keys:
Am I missing something in the original question?
Janko