Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

hascode & equal?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Fred, Campbell, Mohamed: Thank You. I will remember the comments.
 
Campbell Ritchie
Marshal
Posts: 76464
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
permaculture is largely about replacing oil with people. And one tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic