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