1

I have two different objects of same entity “Community”

And two objects(community and com) have same values

Communty.java have following variables:

   private Integer communityId;
   private String communityName;
   private String description;

   // many to many relationship
   private Set<Faculty> faculties = new HashSet<Faculty>();
   private Set<User> users = new HashSet<User>();

and I used equal method as:

@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();
   }

When I checked community==com , it returns false .. why? What mistake I have done? Both the objects are retrieved from the database!

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
schoolcoder
  • 1,546
  • 3
  • 15
  • 31
  • possible duplicate of [what is the difference between == operator and equals()? (with hashcode() ???)](http://stackoverflow.com/questions/4505357/what-is-the-difference-between-operator-and-equals-with-hashcode) – McDowell Dec 27 '12 at 11:26
  • whether u had overrided public int hashCode() – swamy Dec 27 '12 at 11:46
  • no.. i have not overrided hashCode() but using equals() in the return statment of the equals() defined above worked! – schoolcoder Dec 27 '12 at 11:50

8 Answers8

8

== compares links to the objects.

You should explicitly call community.equals(com). Also take care of the null checks.

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • it still says false after using community.equals(com) and community.getCommunityId() and com.getCommunityId are same! – schoolcoder Dec 27 '12 at 11:30
  • you should check my answer for the possible reason – Rahul Dec 27 '12 at 11:32
  • Try to change your last statement in your `equals` method to `return this.getCommunityId().equals(community.getCommunityId());` As stated in other answer, `Integer`s, unlike `int`s also require `equals` method to be compared correctly. – Vi. Dec 27 '12 at 11:32
3

Because you're comparing objects (the IDs) using == rather than equals(). == tests if both variables reference the same object. equals() tests if the two variables reference two functionally equal integers (i.e. with the same int value).

It's almost always a bug to compare objects using ==, except for enums.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

== compares the two references which may be pointing to two different locations irrespective of the object content.

You should use community.equals(com) to check for equality'

Also, your equals method contains the following segment :

community.getCommunityId() == this.getCommunityId()

as communityId is an Integer object, the == operator may give negative result for integer values which are not in the range [-127, 128] due to interning, thats a separate concept, you can check it later.

You need to use equals() there as well or compare the .intValue()

return community.getCommunityId().equals(this.getCommunityId())
Rahul
  • 15,979
  • 4
  • 42
  • 63
1

Because, they don't refer same object. == used to check, whether both referring same object.

== for object refer to same.

equals content equivalency.

try this

return community.getCommunityId().equals(this.getCommunityId());
Ravi
  • 30,829
  • 42
  • 119
  • 173
1

The problem of your equals method is that you are using == operator for Objects. And here the CommunityId must be the same object in order to return true:

 return community.getCommunityId() == this.getCommunityId();

It should be

 return community.getCommunityId().equals(this.getCommunityId());
aphex
  • 3,372
  • 2
  • 28
  • 56
0

When I checked community==com , it returns false .. why

This means; are these two references exactly the same. i.e. to the same object. What you intended was

boolean equal = community.equals(com);

BTW Your if (obj == null) check is redundant.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

== operator in Java compares the memory address of the two objects,in your case comm and community must be two different objects stored at two different memory addresses

Nargis
  • 4,687
  • 1
  • 28
  • 45
0

You are comparing two distinct objects communityId's. is it necessary to declare communityId as Integer? because Integer is an object. Why don't you simply declare communityId with a primitive type int; int communityId should work.

Risin
  • 21
  • 3