4

Possible Duplicate:
java String concatenation

Sources tell us that concat is implemented as follows:

   public String concat(String str) {
        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);
    }

Does + implementation differ when it comes to Strings? How? Is there a performance difference between + and concat. When should one be chosen over another?

Community
  • 1
  • 1
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    Before someone closes this, note, that the question is not whether `+` is the same as `concat`, but rather deals with specific `+` implementation. Thank you – James Raitsev Dec 31 '12 at 01:10
  • Which, as the linked answer states, is done with `StringBuilder` and it's `append` method. It also address your performance consideration questions. – Anthony Accioly Dec 31 '12 at 01:18
  • 2
    Actually, I'm of the opinion that the _question_ has to be a duplicate to be closed-as-dupe. The fact that it may be answered in a totally unrelated question in no way makes the _question_ a dupe. Note that I'm not saying this _isn't_ a dupe question, just that the "answered elsewhere" reasoning for closing as a dupe is flawed - it should be "asked elsewhere". – paxdiablo Dec 31 '12 at 01:21

1 Answers1

7

This is a test I just made:

I created a class with those 3 instructions:

    String s1 = "foo";
    String s2 = "bar";
    String s3 = s1 + s2;

Then I took the generated .class file and I decompiled using JAD decompiler. This is how the code show up in the regenerated source:

    String s = "foo";
    String s1 = "bar";
    String s2 = (new StringBuilder()).append(s).append(s1).toString();

So: this is the difference between + and concat.

I guess concat() is always better than StringBuilder, because it requires less objects to be created. You may chose StringBuilder if you want to append string repeatedly in a loop; in this case concat may create a new String each time, while StringBuilder may just expand the internal buffer. But, if StringBuilder is best in this last scenario, we can say that still concat() is better than +, in loops.

Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
  • 3
    +1 for a clever approach. but should probably note that this is implementation dependent, there is nothing in the Java spec AFAIK to stop + being implemented with concat when there are two String inputs – mikera Dec 31 '12 at 01:20
  • [JLS 15.18.1](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1) specifically says it's implementation-dependent. But a StringBuilder will quickly become better than `concat()` because `concat()` needs to copy all of the previous chars, meaning that if you're concatting 5 strings, the first one will have its chars copied 4 times (the second one 3 times, etc). With StringBuilder, there's only one copy per String. – yshavit Dec 31 '12 at 03:15