• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() in java.util and equals() in Object

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. What is the difference in equals() in java.util and equals() in Object class?
2. Why equals() in java.util overrides equals() in Object ?.
3. Code:
Box b1 = new Box();
Box b2 = new Box();
b1.l = 5 ; b2.l = 5 ; // l is public instance var in box class
b1.equals(b2) is false // Because 2 different objects hence 2 different references stored in b1 and b2. (Please correct me if iam wrong)
But..
Integer i1 = new Integer(5);
Integer i2 = new Integer(5);
i1.equals(i2) returns true!. Why? If I apply same logic as above, why i1.equals(i2) returns true? OR is implementation of equals method for wrapper class different?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Integer class has its own equals method that overrides objects equals method. The method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object. So it does actually check the value. If you change i2 to 6, false is returned.
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic