-2

Say I have 3 String variables:

String firstName =  "aaa";
String middleName =  "bbb";
String lastName =  "cccc";

To append string variables we can use:

String fullName = firstName + "-" + middleName + "-" +lastName ;

Or we can use StringBuilder:

StringBuilder builder = new StringBuilder();
builder.append(firstName);
builder.append("-");
builder.append(middleName);
builder.append("-");
builder.append(lastName);
String fullName = builder.toString();

When to use StringBulider or when to append it directly?

Dev
  • 13,492
  • 19
  • 81
  • 174
  • 3
    Also, see http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java – TryinHard Aug 18 '15 at 05:01

2 Answers2

2

As a programmer we should always care about the readability of our code and the first case looks quite readable to me than the second one.

Now if your question is which one should I choose ?

Than answer is any of them and you should not bother for this.StringBuilder only make difference with loop, other than that currently both of the cases would be almost same.

akash
  • 22,664
  • 11
  • 59
  • 87
1

Simplest way : If you have access to apache common, you could use StringUtils.join().

Next, if all the Strings could be marked as final, then use + directly as using + on final Strings (which are compile time constants) is actually a compile-time operation.

If, you don't want to do the 2 things above, then use StringBuilder to concatenate strings (Even though + on Strings is done using StringBuilder internally.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • There's no need to mark the strings as `final`, nor any need for external libraries. Java compiles string concatenations into `StringBuilder` calls, making them identical. The only exception is when concatenating a string in a loop or other delayed concatenation operation in which case explicitly using a `StringBuilder` avoids repeated concatenation operations. – dimo414 Aug 18 '15 at 05:05
  • @dimo414 - I know java does that.. You should probably have a look at my last edit.. If there is an external library that already does this, then I think there is no point re-inventing the wheel :) – TheLostMind Aug 18 '15 at 05:07
  • Why use an external library when `+` concatenation already does exactly the same thing? – dimo414 Aug 18 '15 at 05:08
  • 1
    @dimo414 No, the compile-time concatenation will _only_ happen if they're both final (or both the plan string literals). All of the arguments need to be compile-time constants as defined in JLS 15.28. See this gist: https://gist.github.com/yshavit/67a3658d025eb1510ff6 – yshavit Aug 18 '15 at 05:12
  • @dimo414 - `+` behaves differently for final and non-final strings. – TheLostMind Aug 18 '15 at 05:15
  • That's not my point. `+` concatenation compiles into `StringBuilder` calls, making `+` and `.append()` identical. – dimo414 Aug 18 '15 at 05:21
  • @dimo414 - Well, yes, in case of non-final strings. – TheLostMind Aug 18 '15 at 05:22
  • Right. So why are you advocating for external libraries and explicitly using `StringBuilder`? For simple (e.g. non-loop) concatenations like OP's case, `+` is exactly what you need. – dimo414 Aug 18 '15 at 05:24
  • @dimo414 - I was merely suggesting that for a larger data set, he has many options. – TheLostMind Aug 18 '15 at 05:25
  • Your answer says an external library is the simplest way to concatenate strings, and that (aside from compile-time constants) the right alternative is to use `StringBuilder`. You say nothing about "larger data sets", nor is OP asking about them. – dimo414 Aug 18 '15 at 05:29