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

14th Sep 2017, 9:34 AM
Divya Vishwakarma
Divya Vishwakarma - avatar
6 Antworten
+ 3
https://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/ read this
14th Sep 2017, 11:06 AM
AutomatedpROCESSES
AutomatedpROCESSES - avatar
+ 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.
14th Sep 2017, 9:38 AM
AutomatedpROCESSES
AutomatedpROCESSES - avatar
+ 2
but hash table works with equals method + same hascode does not guarantee equal.
14th Sep 2017, 10:10 AM
AutomatedpROCESSES
AutomatedpROCESSES - avatar
+ 2
change, well yes you can override the hashcode() and yes it is not refference on memory it is sort of clustering.
14th Sep 2017, 3:33 PM
AutomatedpROCESSES
AutomatedpROCESSES - avatar
+ 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.
14th Sep 2017, 3:27 PM
buran9
buran9 - avatar
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.
14th Sep 2017, 9:41 AM
Divya Vishwakarma
Divya Vishwakarma - avatar