• Post Reply Bookmark Topic Watch Topic
  • New Topic

hascode & equal?  RSS feed

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have following lines in my code : -


Person p1 = new Person("monty", "jack");
Person p2 = new Person("monty", "jack");

p1 & p2 are not equal. WHY?

Integer i1 = new Integer(25);
Integer i2 = new Integer(25);

i1 & i2 are equal. WHY?
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p1.equals(p2) will return true.. as it checks for the contents of the object...

However, p1 == p2 will return false.. as it checks if the references refer to the same object or not!!

hope this is your issue!!

& operator looks for bitwise equality..

so in case of objects bits of references are checked.. which will be different...

but in case of integers (i.e. primitives) bits will come out to be same.. hence & on int will return 1..

Cheers,
Vicky
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Shilpakar wrote:if i have following lines in my code : -


Person p1 = new Person("monty", "jack");
Person p2 = new Person("monty", "jack");

p1 & p2 are not equal. WHY?

Integer i1 = new Integer(25);
Integer i2 = new Integer(25);

i1 & i2 are equal. WHY?


In case of Person class you need to override the equals(Person p) method and then check for the equality of the contents in that method. And you can read more about overriding hashCode and equals in this thread.
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mahomed,

Please correct me if I am wrong in my comments!! that will help me in learning as well!!
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky chauhan wrote:p1.equals(p2) will return true.. as it checks for the contents of the object...

You would have to override the equals method in the Person class and add corresponding equality tests- checking if firstName and lastName are equal.

vicky chauhan wrote:However, p1 == p2 will return false.. as it checks if the references refer to the same object or not!!

This is correct.

vicky chauhan wrote:
& operator looks for bitwise equality..

so in case of objects bits of references are checked.. which will be different...

but in case of integers (i.e. primitives) bits will come out to be same.. hence & on int will return 1..


And I think the "&" used by the OP doesn't indicate the Bitwise operator. You can read this tutorial to know more about Bitwise operations.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote: . . . I think the "&" used by the OP doesn't indicate the Bitwise operator. . . .
Agree. It is ordinary English.

you should by now know to use code tags, Rahul Shilpakar; they might have prevented that confusion.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing the code of the Person class, we honestly can't say if p1.equals(p2) will return true or false. If equals() is not overridden, then it will wihtout a doubt return false. However, if if it IS overridden, it would still depend on HOW it is overridden.

The Integer class, being part of the API, lets us check and see what exactly the equals() method does, and see that it will return true (in this case).
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Fred, Campbell, Mohamed: Thank You. I will remember the comments.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also say how it returns true and false; under some circumstances you can think you are creating two Integer objects and you are in fact creating one.Integers have special treatment, and small numbers (-128 to 127 or maybe a larger range) are cached, so try that code, then change 25 to 25000 and see whether you get different results.
This applies to Integers, and may apply to all eight wrapper classes, because they all say in the API for their valueOf methods that values may be cached.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!