This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

String, StringBuffer and StringBuilder - Performance  RSS feed

 
Vijayanarayana Reddy Bhoomireddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Can anyone please clarify the difference between String, StringBuffer and StringBuilder classes in terms of performance. Any significant compile time benefits if we use String objects instead of StringBuffer/StringBuilder objects?

Sorry if there is a post on the same topic before.

Many Thanks
Vijay
 
Campbell Ritchie
Marshal
Posts: 64115
215
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The three classes are as different as chalk and cheese.
Don't use StringBuffer; it has a significant performance overhead over StringBuilder and the synchronisation can be replaced by locking if required.

Lots more in the Java� Tutorials. If you want text, use String; it is immutable and thread-safe and doesn't require cloning or defensive copying, so you can use the same String object in different places.
If you need to change the contents of the text, use StringBuilder and get the contents out with its toString() method.
Beware: StringBuilder hasn't got an overridden equals() method, so if you want to see whether two StringBuilders have the same contents try

builder1.toString().equals(builder2.toString())
 
Campbell Ritchie
Marshal
Posts: 64115
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do some repeated concatenation of String like thisThen you can see how poor the performance is and how much faster it is with StringBuilder.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vijayanarayana Reddy Bhoomireddy wrote:All,

Can anyone please clarify the difference between String, StringBuffer and StringBuilder classes in terms of performance. Any significant compile time benefits if we use String objects instead of StringBuffer/StringBuilder objects?

Sorry if there is a post on the same topic before.

Many Thanks
Vijay



String is a class that allows the developer to build immutable String objects, whilst StringBuilder allows the developer to create strings from arrays of characters for example. This example illustrates a subtle security bad practice.



For further information this book helpful in explaining concepts, except for StringBuffer, which might have been excluded due to the performance overhead mentioned in this thread.
 
Hey cool! They got a blimp! But I have a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!