• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer & StringBuilder and equals()

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I hope you are well.
Am I correct in thinking that the StringBuffer and StringBuilder classes DO NOT override the Object equals() method? So their versions of equals() uses "=="? Is this a valid work around?
I am away from my normal PC at present and do not have a JDK installed on this one or I'd check it out myself.

stringbuffer1 = "hi"';
stringbuffer2 = "hi";
System.out.println((stringbuffer1.toString()).equals(stringbuffer2.toString())); // will it print true?

Is it the same situation for StringBuilder?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phillip Wells wrote:Am I correct in thinking that the StringBuffer and StringBuilder classes DO NOT override the Object equals() method?


If you look at the API documentation for those classes you'll see that they both don't override equals(), so they inherit the default equals() from Object, which compares objects using ==.

If you want to check whether two StringBuffers or StringBuilders have the same content, then your workaround (calling toString() on them) is valid.
 
Phillip Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent, thanks very much Jesper.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understand everything what there in tutorial,,,,,,,,,,,,,,,,

but i am not able to point out the difference in between the two............
stringBuffer and StringBuilder
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods in StringBuilder are not thread safe.
 
Phillip Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shanky,
The difference is that StringBuffer's important methods are synchronized, StringBuilder's are not. Lol "snap" Siva
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So be sure to use StringBuilder unless you are working with shared data.
In that case, use StringBuffer.

Jim ... ...
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may now be wondering why we have two classes that do almost the same (StringBuffer and StringBuilder):

StringBuilder is a new class, added in Java 5. It (more or less) replaces StringBuffer, and you should always prefer StringBuilder above StringBuffer - StringBuffer should be regarded as a legacy class.

The difference is, as already said, that most methods in StringBuffer are synchronized, while they are not in StringBuilder. The engineers at Sun realized that for most applications, synchronization is not necessary, but it does add some runtime overhead.

This is similar to Vector and ArrayList, or Hashtable and HashMap.

Jim Hoglund wrote:So be sure to use StringBuilder unless you are working with shared data.
In that case, use StringBuffer.


To make this more precise: If you use it from multiple threads at the same time (which you almost never do), use StringBuffer. In all other cases (almost always), use StringBuilder.
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic