Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

StringBuffer Question

 
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.

 
tumbleweed
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).]
 
Wanderer
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).
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!