This week's book giveaway is in the Testing forum.
We're giving away four copies of Practical Unit Testing with TestNG and Mockito and have Tomek Kaczanowski on-line!
See this thread for details.
The moose likes Ranch Office and the fly likes StringBuffer Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » This Site » Ranch Office
Reply Bookmark "StringBuffer Question" Watch "StringBuffer Question" New topic
Author

StringBuffer Question

Kenneth Ho
Greenhorn

Joined: Mar 22, 2001
Posts: 18
Question 53. in Abhilash Java Quiz
I would like to know whether StringBuffer can be used to compare string. I complied and run the foll codes and it shows nothing.


--------------------<BR>Java will never die.<P>Kenneth
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5087
Originally posted by Frank Carver:
The two StringBuffers don't come out as equal, because StringBuffer just uses the "default" implementation of equals(), which does the same as '==' and directly compares the object references. The two StringBuffers are different objects with different references, so they come out as non-equal.

And
Originally posted by Hari Gangadharan:
I really love this question! The StringBuffer *does* not override Object's "equals" method whereas String do. Hence when you invoke s1.equals(s2), the equals method of Object is invoked. The equals method of Object just does a shallow compare (which essentially is s1 == s2) and returns false!
Hope this helps --



[This message has been edited by Johannes de Jong (edited April 06, 2001).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
The JavaRanch forum in intended for discussion of this web site, not questions about Java itself. That's what all the other forums here are for. I'm moving this discussion to Java in General (Beginner).


"I'm not back." - Bill Harding, Twister
 
 
subject: StringBuffer Question
 
Threads others viewed
Doubt in StirngBuffer
Doubt regarding StringBuffer
StringBuffer equals() query
equals method and StringBuffer
StringBuffer
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com