3

Possible Duplicate:
Is there a difference between String concat and the + operator in Java?

Is there any difference between:

String A = "Hello";
String B = A.concat("Testing");

and

String A = "Hello";
String B = A + "Testing";

Yes i know there ain't any difference in the output. Wanted to know if there is any technical difference. Thanks !

Community
  • 1
  • 1
HashimR
  • 3,803
  • 8
  • 32
  • 49

1 Answers1

3

Not in this example.

If you concatenate several strings with + (e.g., "Hello " + user + "!"), current compilers will use a StringBuilder to implement the + operator because this is faster than first creating the string for "Hello " + user and then creating the final string. They probably don't do that for concat(), so there would be a technical difference here.

Edit: see also String concatenation: concat() vs "+" operator and Is there a difference between String concat and the + operator in Java?.

Community
  • 1
  • 1
Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • `concat` only accepts one string to concatenate anyway, so representing `"Hello" + user + "!"` via `concat` would be cumbersome. I'm fairly sure, though, that .NET's `String.Concat(params String[])` uses a `StringBuilder` as that's what the compiler turns `+` into. – Joey Oct 04 '11 at 10:43