title: Comparing two objects in Java
body: <p>I have two different objects of same entity “Community”</p>
  
  <p>And two objects(community and com) have same values</p>
  
  <p>Communty.java have following variables:</p>
  
  <pre><code>   private Integer communityId;
     private String communityName;
     private String description;
  
     // many to many relationship
     private Set&lt;Faculty&gt; faculties = new HashSet&lt;Faculty&gt;();
     private Set&lt;User&gt; users = new HashSet&lt;User&gt;();
  </code></pre>
  
  <p>and I used equal method as:</p>
  
  <pre><code>@Override
     public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if(obj==null)
                   return false;
            if(obj==this)
                   return true;
            if(!(obj instanceof Community)) return false;
  
            Community community = (Community)obj;
            return community.getCommunityId() == this.getCommunityId();
     }
  </code></pre>
  
  <p>When I checked <code>community==com</code> , it returns false .. why? What mistake I have done? Both the objects are retrieved from the database!</p>