Let's say I have this code in Java:
HashSet<String> wordSet = new HashSet<String>();
String a = "hello";
String b = "hello";
wordSet.add(a);
Would wordSet.contains(b);
return true
or false
? From what I understand, a
and b
refer to different objects even though their values are the same. So contains()
should return false
. However, when I run this code, it returns true
. Will it always return true
no matter where String object b
is coming from as long as b
contains the value "hello"
? Am I guaranteed this always? If not, when am I not guaranteed this? And what if I wanted to do something similar with objects other than Strings?