-4

What is the best way to concatenate two Strings in java. likely best performance and less memory usage. Please help me..!

1 Answers1

2
StringBuilder stringBuilder = new StringBuilder(firstString);
stringBuilder.append(secondString);
String string = stringBuilder.toString();

But according to @a_horse_with_no_name the following is even more efficient

StringBuilder sb = new StringBuilder(firstString.length() + secondString.length()); 
sb.append(firstString); 
sb.append(secondString);
String string = sb.toString();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 2
    `new StringBuilder(firstString.length() + secondString.length()); sb.append(firstString); sb.append(secondString);` should be more efficient –  Nov 05 '14 at 08:38