aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer and equals()" Watch "StringBuffer and equals()" New topic
Author

StringBuffer and equals()

Stephen Pride
Ranch Hand

Joined: Sep 14, 2000
Posts: 121
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


SCJP
Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
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

Joined: Aug 26, 2000
Posts: 10065
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.


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 07, 2000
Posts: 87
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 14, 2000
Posts: 121
Thanks to all.
Regards,
SP
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you, Scott!
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20729
    ∞

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.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer and equals()