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!

3
  • possible duplicate of what is the difference between == operator and equals()? (with hashcode() ???) Commented Dec 27, 2012 at 11:26
  • whether u had overrided public int hashCode() Commented Dec 27, 2012 at 11:46
  • no.. i have not overrided hashCode() but using equals() in the return statment of the equals() defined above worked! Commented Dec 27, 2012 at 11:50

8 Answers 8

8

== compares links to the objects.

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

Sign up to request clarification or add additional context in comments.

3 Comments

it still says false after using community.equals(com) and community.getCommunityId() and com.getCommunityId are same!
you should check my answer for the possible reason
Try to change your last statement in your equals method to return this.getCommunityId().equals(community.getCommunityId()); As stated in other answer, Integers, unlike ints also require equals method to be compared correctly.
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.

Comments

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())

Comments

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

Comments

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

Comments

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.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.