• 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

[Possible] String Concatenation Improvements

 
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everybody knows that the string concatenation operator + and += are absurdly inefficient due to StringBuilder instances being created all the time. That is:

creates a new StringBuilder instance every loop iteration.

My question is, why is it that the String class itself does not already implement the self-resizing behaviour of StringBuilder? Is it a good thing that String is immutable, or should it have a sort of "immutability switch" such that string literals are always immutable and String instances may be made immutable, a little similar to const-ness in C++?

Alternatively, would it be of any benefit if each thread had its own "background" StringBuilder that gets reused each time string concatenation occurs instead of making a new one every time?

Const-ness in Java would be cool...
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is const a real OO concept? If you make an object mutable, do you want people making it immutable elsewhere? And how would you implement that?

The + operator is very efficient; you can use it as often as you like, as long as it is all in the same statement. It is only inefficient when used repeatedly.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Louis Denning wrote:Everybody knows that the string concatenation operator + and += are absurdly inefficient due to StringBuilder instances being created all the time. That is:

creates a new StringBuilder instance every loop iteration.

My question is, why is it that the String class itself does not already implement the self-resizing behaviour of StringBuilder?



Or alternatively, how about just using the string builder directly?



Henry
 
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

Louis Denning wrote:Is it a good thing that String is immutable, or should it have a sort of "immutability switch" such that string literals are always immutable and String instances may be made immutable, a little similar to const-ness in C++?


Yes, it is a good thing that String is immutable. It makes certain optimizations possible such as the string pool. Immutability in general is good because it makes multi-threaded programming much easier - you never have to think about other threads modifying an immutable object. It's too bad that many other classes such as java.util.Date and java.util.Calendar are not immutable. The designers of the Java programming language in the 1990's probably weren't very much aware of this, because multi-core processors and multi-threaded programming were not so common back then.

StringBuilder is already more or less a mutable String. So, if you absolutely need an object that's mutable, use StringBuilder instead of String - like Henry showed.

There are static analysis tools such as FindBugs and also in IDEs (IntelliJ has great static analysis tools) that will warn you for inefficient constructs like concatenating strings in a loop.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic