title: How use of == operator brings in performance improvements compared to equals?
body: <p>In Effective JAVA by Joshua Bloch, when I was reading about static factory methods , there was a statement as follows </p>
  
  <blockquote>
    <p>The ability of static factory methods to return the same object from
    repeated invocations allows classes to maintain strict control over
    what instances exist at any time. Classes that do this are said to be
    instance-controlled. There are several reasons to write
    instance-controlled classes. Instance control allows a class to
    guarantee that it is a singleton (Item 3) or noninstantiable (Item 4).
    Also, it allows an immutable class (Item 15) to make the guarantee 
    that no two equal instances exist: a.equals(b) if and only if a==b. If
    a class makes this guarantee, then its cli- ents can use the ==
    operator instead of the equals(Object) method, which may result in
    improved performance. Enum types (Item 30) provide this guarantee.</p>
  </blockquote>
  
  <p>To investigate how == operator brings in performance improvements ,
  I got to look at  <a href="https://upsource.jetbrains.com/lib/view/jdk7u10/java/lang/String.java;nav:0:0:focused" rel="noreferrer">String.java</a></p>
  
  <p>I saw this snippet</p>
  
  <pre><code>public boolean equals(Object anObject) {
          if (this == anObject) {
              return true;
          }
          if (anObject instanceof String) {
              String anotherString = (String) anObject;
              int n = value.length;
              if (n == anotherString.value.length) {
                  char v1[] = value;
                  char v2[] = anotherString.value;
                  int i = 0;
                  while (n-- != 0) {
                      if (v1[i] != v2[i])
                              return false;
                      i++;
                  }
                  return true;
              }
          }
          return false;
      }
  </code></pre>
  
  <p>By performance improvement what does he mean here ? how it brings performance improvement .</p>
  
  <p>Does he mean to say the following </p>
  
  <p><em>If every class can assure that a.equals(b) if and only if a==b , it means it brings in an indirect requirement that there cannot be objects referring to 2 different memory spaces and still hold the same data , which is memory wastage . If they hold same data they are one and the same object .That is they point to same memory location.</em></p>
  
  <p>Am I right in this inference ?</p>
  
  <p>If I am wrong can you guide me in understanding this ?</p>