This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Yet Another "StringBuilder vs +" Question  RSS feed

 
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, most of us have probably seen this class of question before, but I've got something else to ask: Is StringBuilder faster than "+" even outside of a loop?

Exempli gratia:vs what it translates intovs another way of writing itvs with a predetermined expected length
I expect the first two to be equivalent, for the latter is translated from the former during compilation. I would expect the third one to have an internal char buffer of some default size (probably 8, I believe) that expands (probably doubling) as needed. The last one hopefully allocates enough space for everything without needing to expand the internal buffer.

My questions now are:

(1) Would the fourth case be non-negligibly faster than the other three, where there is probably buffer expansion to do?
(2) When would it be justifiable to use each of the cases?
(3a) Would most JVMs be smart enough to optimize this "unnecessary" buffer expansion away, perhaps into something like the fourth case?
(3b) Are there any JVM options that do this kind of optimization?
(3c) How might Java 9's "String Indification" handle this?
(4a) Is the following practical in the slightest:(4b) Less functionally......though not very convenient in handing primitives.
(5) Where'd the Edit button go?
 
Bartender
Posts: 9493
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Denning wrote:(1) Would the fourth case be non-negligibly faster than the other three, where there is probably buffer expansion to do?


No. Considering compiler optimizations that you ask about in the following questions, the minimal possible gain in performance is *never* worth it compared to the time gained by another developer understanding the code written in the first place.

(2) When would it be justifiable to use each of the cases?


None but the most clear is justifiable.

(3a) Would most JVMs be smart enough to optimize this "unnecessary" buffer expansion away, perhaps into something like the fourth case?


This question is unimportant. Even if a compiler could do this, the time to implement this is a lot more than the time saved.

(3b) Are there any JVM options that do this kind of optimization?


No. Such an optimization would be performed through syntactic 'lifting'. It would never reach the virtual machine.

(3c) How might Java 9's "String Indification" handle this?


Dunno. I still have to read about Java 9.

(4a) Is the following practical in the slightest:


No. You're undermining the compiler from making natural optimizations. Stick to what make makes sense conceptually. The + operator clearly conveys the intent to concatenate two strings. How it's implemented should not be a worry for the programmer, unless it happens within a loop, and even then a smart compiler can often optimize it away.

(5) Where'd the Edit button go?


There's a topic about this. Check the Ranch Office board.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Denning wrote:...though not very convenient in handing primitives.


Hunh? Why would you want to handle a primitive with a StringBuilder?

StringBuilders are specifically for concatenating Strings (and maybe chars) and, as you can read about here, Strings should only be used for text ... or display.

HIH

Winston
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Louis Denning wrote:...though not very convenient in handing primitives.


Hunh? Why would you want to handle a primitive with a StringBuilder?

StringBuilders are specifically for concatenating Strings (and maybe chars) and, as you can read about here, Strings should only be used for text ... or display.


I disagree. StringBuilders are for building Strings with any possible type of content, not concatenating just Strings. There's a reason the append method is overloaded so many times. Text (for display or otherwise) can also contain numbers and booleans.

That article you mention is not relevant to the StringBuilder - only to the built Strings. So yes, you can abuse StringBuilder to build Strings where Strings should not be used, but don't blame the StringBuilder for that - it's just a tool.


Regarding using an explicit StringBuilder, my rule of thumb is to only use an explicit StringBuilder if you have either repeated concatenation, or several conditional concatenations; for simple one-liners like in the opening post I just use +. And in those cases where I do use a StringBuilder I rarely bother with calculating the required length beforehand. Either I make a guess and deal with that guess being off, or I just stick to the default initial size. In both cases the StringBuilder will grow if it needs to.
 
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:...for simple one-liners like in the opening post I just use +.


I wonder when it's okay to use String's concat() method. I think its okay to use it for one-liners too. Isn't it?
 
Rob Spoor
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use concat if you have exactly two Strings that you want to concatenate. It's more efficient than using + because it doesn't need a StringBuilder. To be honest, I can't remember ever using it though.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:I disagree. StringBuilders are for building Strings with any possible type of content, not concatenating just Strings. There's a reason the append method is overloaded so many times. Text (for display or otherwise) can also contain numbers and booleans.


True, but the only reason for converting from another type is to render it in some sort of human-readable form; either for persistence, data-transfer, or display - ie, it should be at the "edge" of your application.

And IMO, there was absolutely no need to overload all those methods, except for types - like arrays - that can take an offset, and possibly char, since I'd care to bet that almost every one of them uses String.valueOf(Object).
The only reason I can think of was that they wanted StringBuilder to look exactly like StringBuffer (which was arguably already overblown); and if that was the case, why didn't they add a StringContainer interface?

That article you mention is not relevant to the StringBuilder - only to the built Strings. So yes, you can abuse StringBuilder to build Strings where Strings should not be used, but don't blame the StringBuilder for that - it's just a tool.


I'm not. I'm simply saying that there's a time and a place to use it; and if you're "building up" Strings that are so vast or complex (and you're doing it so often) that whether you use '+' or concat() is a performance issue, you may want to consider whether (a) what you're doing is appropriate, or (b) whether there might be another alternative - like String.format().

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!