This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer Question  RSS feed

 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!