• 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

String / StringBuffer and equals

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
consider the following code:
String s = new String("abc");
StringBuffer sb = new StringBuffer("abc");
The expression sb.equals(s) is false, but why is s.equals(sb) false?
I know that StringBuffer does not override equals, but String does. So I expected that s.equals(sb) calls the overloaded String method which goes after the contents of sb, not the reference address itself.
Any ideas?
Thanks,
Bernd
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Equals on StringBuffer does a shallow comparison. (same like ==) Will return true only if the objects are same.

Clement
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. I always tell myself when I see a StringBuffer and I want to compare a String to it's contents, I have to use toString() on the StringBuffer first.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals method takes an Object as a paramater (because it's defined in the Object class), but, in general, if you call equals on one object type and pass it another object type, it'll always come back as false.
It just doesn't make sense for two entirely different types of objects to be "equal." However, had you done this:

Now you'd be comparing a String object with a String object, which makes more sense than comparing a String object with a StringBuffer object.
I hope that helps,
Corey
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

It just doesn't make sense for two entirely different types of objects to be "equal." However, had you done this:


Maybe another way to think about it, if it helps to resolve the confusion, is this:
Sun had something in particular in mind when they created the 'equals' method. There is a moral contract it is expected to obey. That contract is "only return true if the implementation of the two objects is equivalent".
That isn't the same as "only return true if the two objects could be thought of has having equivalent values for some particular purpose".
The Sun notion of equals is more stringent, and really better from an OO programming standpoint. It allows you to have a reasonable expectation that you could use X and Y interchangeably in a wide variety of situations, although there are some limitations to that.
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
thanks for your feedback, this is very helpful for me.
-Bernd
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rule of thumb:
(StringBuffer).equals(String) OR
String.equals(StringBuffer)
both always return false
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic