• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Possible] String Concatenation Improvements  RSS feed

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!