• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

StringBuffer and equals()

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since equals() is a method in Object, StringBuffer inherits it, but it appears it doesn't do anything with it (i.e., override it to make its own implementation). Is this correct? From the books I've seen, the StringBuffer class doesn't list equals() as one of its methods.
Regards,
SP
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Stephan u r right. The StringBuffer class does not override the equals method.
So using equals method with a StringBuffer always returns "false"
Aruna.
[This message has been edited by Aru (edited September 21, 2000).]
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only small correction: StringBuffer's equals method doesn't always return false. Look at this funny example:

prints "the same"
BUT

prints "not the same" - directly the opposite to equals method sematics.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mapraputa:
you are right, can you explain that?
I tried
StringBuffer s=new StringBuffer("aaa");
StringBuffer s1=new StringBuffer("bbb");
s.equals(s1) return false....
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,
the code I tried are:
StringBuffer s=new StringBuffer("aaa");
StringBuffer s1=new StringBuffer("aaa");
s.equals(s1) return false
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since StringBuffer doesn't override equals(Object) we get the base behavior 'if (this == obj)' in Object.equals(Object). StringBuffer.equals() CAN return true when you pass the same reference. Since StringBuffer.append() returns 'this', your code returns true.
[CODE]
StringBuffer b1 = new StringBuffer("abc");
StringBuffer b2 = new StringBuffer("abc");
if (b1.equals(b1)); // always true
if (b1.equals(b2)); // always false b2 is not ref to b1
if (b1.equals(b1.append("def"))); // always true ref == ref
b2 = b1;
if (b1.equals(b2)); // now same ref so true!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, it's good info i got that Stringbuffer doesn't provide 'equals' method and it returns TRUE only when both objects refer to same object. I didn't know that before!!
thanks to all
Ashish
 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all.
Regards,
SP
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Scott!
 
Trailboss
Posts: 24081
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gavin,
I need you to take a look at this thread: http://www.javaranch.com/ubb/Forum10/HTML/000180.html
I would have e-mailed you, but when you are unregistered, I don't know your e-mail address.
 
reply
    Bookmark Topic Watch Topic
  • New Topic