I read somewhere that hashCode is conversion of internal memory address in int type according to javadocs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I read somewhere that hashCode is conversion of internal memory address in int type according to javadocs

so how do we get the same hashCode what I mean is that memory address of two objects String str1 = new String("hello"); String str2 = new String("hello"); are different, so why we get the same hashCode?

25th Apr 2020, 6:21 AM
Shubham Pandey
Shubham Pandey - avatar
3 Answers
+ 1
As far is I know, hash code has nothing to do with memory. It is just a 32 bit integer number which is returned based on where the data is stored in the hash table. If the equals method returns true then the hashCode() will also be similar for that objects. But if you claim that it returns internal memory then why does str1==str2 returns false? Hope you get it. https://code.sololearn.com/c2qIvHkth1Cs/?ref=app
25th Apr 2020, 7:28 AM
Avinesh
Avinesh - avatar
0
Yes thanks , this means the answer which I read on quora is wrong for which I asked this question https://www.quora.com/What-is-hashCode-in-Java-How-could-you-explain-it-in-the-simplest-way/answer/Irfan-Syed-40?ch=10&share=29024e98&srid=uC2AoJ you can refer this he says that hashCode is conversion of some kind of internal address of object
25th Apr 2020, 7:37 AM
Shubham Pandey
Shubham Pandey - avatar
0
From what I know, hash function algorithms have no more specification than produce unique values for referencing / differentiate object... implementations could differs, or even be customized: technically nothing prohibit the use of memory address for this purpose (as is, or as base), but also nothing force to do so ;)
25th Apr 2020, 9:40 PM
visph
visph - avatar