• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer and equals()

 
Stephen Pride
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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).]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • 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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
scott irwin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks to all.
Regards,
SP
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Scott!
 
paul wheaton
Trailboss
Pie
Posts: 21735
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic