This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Here if the comment the line 1 then i getting output as one 3
but if i uncomment that line then it is giving null 3
Here i thought if hashcode() is not overridden then it will store all the values in the same bucket. So while retriving the values it checks its equals method. Here (if uncommented the line 1) it return true for ob3 also then why not it is printing three?
Please clear my doubt
geeta vemula wrote:Here i thought if hashcode() is not overridden then it will store all the values in the same bucket.
No! It will just do the opposite. Since the hashCode method is not overridden, so it will place all values in different buckets as hashCode method of Object class mostly returns unique values for objects...
ok then if i comment out the line 1 then how it is printing one . it should not search in that particular bucket?? if it is searching that bucket means it is also searching bucket with key ob3, there also equals method is returning false.
Yes you are right that obj1 and obj3 are equal according to the equals method. But since the hashCode method is not overridden, so the hashCode method of object class will be called. It will return different hashCode for obj1 and obj3. That's what the whole point is. obj1 and obj3 will go into different bucket...