title: Test of equality (equals and hash code method)
body: <p>As per the below link</p>
  
  <p><a href="https://stackoverflow.com/questions/1990734/hashcode-and-equals">Hashcode and equals</a></p>
  
  <p><strong>So it is assumed that if 2 objects are equal (that is, equals() returns true), then their hashCodes() must return the same value.</strong></p>
  
  <p>But consider the below example </p>
  
  <pre><code>public class Test {
  
      public static void main(String args[]) {
          Demo d1 = new Demo(123);
          Demo d2 = new Demo(123);
  
          System.out.println("d1.hashCode()--&gt;" + d1.hashCode());
          System.out.println("d2.hashCode()--&gt;" + d2.hashCode());
          System.out.println(d1.equals(d2));
      }
  }
  
  class Demo {
  
      private int name;
  
      Demo(int name) {
          this.name = name;
      }
      @Override
      public int hashCode() {
          Double d = Math.random() * 1000;
          return d.intValue();
      }
      @Override
      public boolean equals(Object o) {
          if ((o instanceof Demo) &amp;&amp; (((Demo) o).getName() == this.getName())) {
              return true;
          } else {
              return false;
          }
      }
      public int getName() {
          return name;
      }
  }
  </code></pre>
  
  <p>the output of the above program is </p>
  
  <blockquote>
    <p>d1.hashCode()-->85</p>
    
    <p>d2.hashCode()-->692</p>
    
    <p>true</p>
  </blockquote>
  
  <p>Here the question is even though the hash code is different the equals method return true.
  So does it mean for equality of object we do not need same hash code</p>