Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode()............. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode()............." Watch "hashcode()............." New topic
Author

hashcode().............

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


here two different objects are created.
they are meaningfully equal so output is true.
if two objects are equal the hashcode should be same....?

contract is only for collections or anywhere?

how hashcode should be same?
[ November 12, 2008: Message edited by: Ganeshkumar cheekati ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Yes if the two objects are equal according to equals() then the hashcode must also be equal.
these methods are Object's methods, not Collections.

Preparing Scjp 1.5
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes those two methods belongs to Object class.

if we override equals() then we should override hashcode().

if we cant override then it wil giv unreliable results.

i havent override equals() in my code but output is true.....

why?
[ November 12, 2008: Message edited by: Ganeshkumar cheekati ]
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
post your code.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362

this is my code....
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
you must override them in order to deal with collections to check the objects are equal or not and hashcode too. Because some collections are storing datas according to the hash code value(Hashing algorithms). for eg. hashtable,HashMap,HashSet.

i hope this clears you.

Preparing Scjp1.5
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
ok i got it...

the String class and the wrapper classes have overridden
the equals() method (inherited from class Object), so that you could compare
two different objects (of the same type) to see if their contents are meaningfully
equivalent.
 
GeeCON Prague 2014
 
subject: hashcode().............