Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

14
  • Ok thx, but what about performance, most interested this part of the question? Commented Nov 23, 2012 at 13:51
  • 3
    @GanGnaMStYleOverFlowErroR: No, it wouldn't. In fact, in this case it would generate fewer strings, because (for the third time) the concatenation would be done at compile-time. And again, even if it weren't, the compiler would do the right thing. Please read yoda.arachsys.com/java/strings.html and if you have a specific situation in mind, compile it and then use javap to see what would actually happen. Commented Nov 23, 2012 at 14:00
  • 1
    @enjoyLife: No, you'll still get the string constants with StringBuffer or StringBuilder. Please read the link in an earlier comment... if you're providing everything in a single expression, then using concatenation will be at least as efficient. StringBuilder is better for things like loops, where you can't express the whole operation in a single expression. Commented Nov 23, 2012 at 14:07
  • 1
    +1 Just because using StringBuilder direct is more complicated won't make it more effficient. Using + will either a) be simplified at compile time, or b) do exactly the same thing with less code. In any case, no matter how large the string generated is, it is unlikely to matter compared with the cost of passing, parsing, and performing the SQL. Commented Nov 23, 2012 at 14:18
  • 1
    @enjoyLife: Using concatenation, the compiler gets to to what it can at compile-time, and do the rest using a StringBuilder at execution time. So it's at least as efficient, and more readable. Commented Nov 23, 2012 at 14:50