Hacker News new | past | comments | ask | show | jobs | submit login

It does... but only in one statement.

    String foo = "foo";
    foo += "bar";
That is under the covers...

    String foo = "foo";
    foo = new StringBuilder(foo).append("bar");
But... if you do:

    String foo = "foo";
    foo += "bar";
    foo += "qux";
that becomes

    String foo = "foo";
    foo = new StringBuilder(foo).append("bar");
    foo = new StringBuilder(foo).append("qux");
So, you've still created the strings: "foo", "foobar", and "foobarqux" and also a pair of StringBuilders.

The actual code was more complicated (and bigger strings), but the issue is that each statement is its own StringBuilder.

That prompted me to explore it and I looked at the byte code invocations at http://shagie.net/2014/08/17/what-goes-on-behind-the-scenes-...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: