• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

equals() in Wrapper Classes

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought equals() method was overriden (ie. was performing "deep" comparison) only in String and Boolean wrapper classes.
Now I encountered an example in Poddar Questions where Double seems to overriding equals() as well...
What is finally the list of all wrapper classes overriding Object's equals() method?
Thank you!!
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of them do. They implement Comparable as well.
- Peter
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do equals() method in String class and equals()
method in the Wrapper class have functionality
i.e they compare the actual values and not if the references are same.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() method for a wrapper class also compares whether the operands are of same Wrapper type, other than checking for their contined values.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the equals method is overriden in all wrapper classes and String class as well. But remember the StringBuffer class doesnt overrides it. Consider the following code
 
Stanislava Trajlov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so all of the following wrapper classes: Boolean, String, Double, Integer, Float, Byte, Short and Long DO OVERRIDE Object's equals() method (and therefore do "deep" comparios) while it's ONLY StringBuffer class that doesn't override it and for StringBuffer objects equals() and == have the same effect.
Is that right?
Thanks!!!
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right. Yes for StringBuffer == and equals have the same effect. I just included the StringBuffer because I thought that its related to this topic, but StringBuffer is not a wrapper class.
 
Stanislava Trajlov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the help!
Stanislava
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If StringBuffer does not implement equals, why are the two lines below from your code TRUE !!
System.out.println(sb.equals(sb)); //true System.out.println(sb1.equals(sb1)); //true
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As StringBuffer does not override the equals() method of the Object class. It has theinherited behaviour of the equals() in the Object. Which simply does a == comparison. So sb==sb and sb1==sb1 will return true and so will the equals method.
As in
reply
    Bookmark Topic Watch Topic
  • New Topic