0
String s1 = new String ("Hi"); String s2 = new String("Hi"); Why s1 == s2 returns false ??
It should return true as both references have same hashCode number. But it returns false. Why ????? https://code.sololearn.com/caZGZ3A4oaPo/?ref=app
6 odpowiedzi
+ 3
https://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/ read this
+ 2
because s1 and s2 are objects and they have allocated different parts of memory and == compares this reference. you have to use method equals() to get true.
+ 2
but hash table works with equals method + same hascode does not guarantee equal.
+ 2
change, well yes you can override the hashcode() and yes it is not refference on memory it is sort of clustering.
+ 1
hmm i heard that hashcod it's not a referance on memory this is just unique int number and it's possible to change it. Second, for equal objects (belong to one class with equal fields) hashcode must be the same, but not vice versa - if hashcode for 2 objects are equal it DOESN'T meen that objects (it's fields) are equal.
0
So what does a hashCode tell about. It tells that the memory location to which a reference is refering to. So here s1 and s2 are refering to same memory location, so it should return true.