• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Strugbuffer Equals?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does a1.equals(b1) in the following code evaluate to false when contents are equal?
When do 2 stringbuffers .equal return true?

class C {
public static void main (String[] args) {
StringBuffer a1 = new StringBuffer ("red");
StringBuffer b1 = new StringBuffer ("red");
System.out.println(a1.equals(b1));
}}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the StringBuffer API, you will see that StringBuffer does not provide its own equals method implementation. It inherits it from the Object class.

Why it does not provide an implementation of equals is an anomaly of Java that is beyond my understanding.

Ive just noticed that StringBuilder (Java 1.5) does not provide an implementation of equals either.
[ October 30, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry, I understand why Java 1.5 does not provide an overridden equals() in StringBuffer: existing code may break. Some code, however dubious, may exist that relies on the present implementation of equals(). Unfortunately, the implication is that this implementation for StringBuffer.equals(), which seems counterintuitive, will never be appealing to our intuition.

As I tell new programmers, my first rule of maintenance and enhancement programming is "Do No Harm".
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"J", it was StringBuilder in Java 1.5 I was referring to, that's a new class based on StringBuffer. StringBuilder is meant for single threaded use.
On thinking about it, Sun recommends that it be used as a drop-in replacement for StringBuffer, so if it suddenly overrode the equals method there would also be a couple of suprises.
Anyway, I agree with your point of view on software maintenance.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic