title: Role of == operator in Java checking memory address
body: <p>Till today I had a belief that Two objects having the same hashcode means both of them have same memory location. But below piece of code tells altogether a different story :</p>
  
  <p>Student Entity:
  public class Student implements Comparable  {</p>
  
  <pre><code>int id;
  int marks;
  String Subject;
  
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }
  public int getMarks() {
      return marks;
  }
  public void setMarks(int marks) {
      this.marks = marks;
  }
  public String getSubjects() {
      return Subject;
  }
  public void setSubject(String subject) {
      Subject = subject;
  }
  
  public Student() {
  
  }
  public Student(int id, String subject, int marks) {
      super();
      this.id = id;
      this.marks = marks;
      Subject = subject;
  }
  @Override
  public int compareTo(Student o) {
      if (this.getId()&gt;(o.getId()))
      return 1;
  
      if (this.getId()&lt;(o.getId()))
      return -1;
  
      return 1;
  }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((Subject == null) ? 0 : Subject.hashCode());
          result = prime * result + id;
          result = prime * result + marks;
          return result;
      }
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Student other = (Student) obj;
          if (Subject == null) {
              if (other.Subject != null)
                  return false;
          } else if (!Subject.equals(other.Subject))
              return false;
          if (id != other.id)
              return false;
          if (marks != other.marks)
              return false;
          return true;
      }
  
  
  }
  </code></pre>
  
  <p>Entering Duplicates in Hashmap to check whether overriding equals &amp; hashcode() works :</p>
  
  <pre><code>public class TestHashSet3 {
  
      static Student s1;
      static Student s2;
      static Student s3;
  
      public static void main(String[] args) {
          setStudent();
          testSet();
          testMap();
   }
  
      static void testMap() {
          Map&lt;Student, String&gt; empMap = new HashMap&lt;&gt;();
          empMap.put(s1, "Arun");
          empMap.put(s2, "Shivam");
          empMap.put(s3, "Varun");
          System.out.println("HashMap executed = ");
          for (Map.Entry&lt;Student, String&gt; obj : empMap.entrySet()) {
              System.out.println(obj  +  " ");
          }
      }
  
      static void testSet() {
          Set&lt;Student&gt; set = new HashSet&lt;&gt;();
          set.add(s1);
          set.add(s2);
          set.add(s3);
          System.out.println("HashSet executed = ");
          for (Student student : set) {
              System.out.println(student  +  " ");
          }
  
      }
  
      static void setStudent() {
          s1 = new Student(124, "Maths", 50);
          s2 = new Student(124, "Maths", 50);
          s3 = new Student(124, "Maths", 50);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Oh3L5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oh3L5.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/SmGlm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SmGlm.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/SfRBd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SfRBd.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/2qntF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2qntF.png" alt="enter image description here"></a></p>
  
  <p>In last screenshot, we can see  that this ==obj turns out to be false. But why ?</p>