String concatenation versus StringBuffer

Discussion in 'Java' started by Darren, Jul 27, 2004.

  1. Darren

    Darren Guest

    Can someone deny or confirm that the latest sdks convert code like

    "abc" + "def" + "ghi"

    into the appropiate StringBuffer code

    new StringBuffer( "abc" ).append( "def" ).append( "ghi" )

    automatically? Or is this some dream I had?

    Thanks!
    Darren, Jul 27, 2004
    #1
    1. Advertising

  2. Darren

    Eric Sosman Guest

    Darren wrote:
    > Can someone deny or confirm that the latest sdks convert code like
    >
    > "abc" + "def" + "ghi"
    >
    > into the appropiate StringBuffer code
    >
    > new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
    >
    > automatically? Or is this some dream I had?


    It was a dream.

    javac does the concatenation of string literals and
    `final' strings at compile time rather than at run time,
    so your example just becomes "abcdefghi" with no StringBuffer
    involvement at all.

    If the concatenated pieces aren't all constants, javac
    generates StringBuffer operations that are just a little
    bit different from your sample. For example,

    "abc" + anyString + "ghi";

    produces the equivalent of

    new StringBuffer()
    .append("abc").append(anyString).append("ghi")
    .toString()

    Oddly enough, compile-time concatenation of constant
    strings doesn't always take place when non-constants are
    also involved. For example,

    "aaa" + "bbb" + anyString + "xxx" + "yyy"

    becomes

    "aaabbb" + anyString + "xxx" + "yyy"

    where the first two pieces have been concatenated by the
    compiler but the final two have not. The compiler is
    presumably being scrupulous about the left-to-right
    associativity of the `+' operator, refusing to treat
    the original as

    "aaa" + "bbb" + anyString + ("xxx" + "yyy")

    which does indeed become

    "aaabbb" + anyString + "xxxyyy"

    You can answer many questions of this sort for yourself
    by disassembling the bytecodes with the javap program.

    --
    Eric Sosman, Jul 27, 2004
    #2
    1. Advertising

  3. Darren

    Grant Wagner Guest

    Darren wrote:

    > Can someone deny or confirm that the latest sdks convert code like
    >
    > "abc" + "def" + "ghi"
    >
    > into the appropiate StringBuffer code
    >
    > new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
    >
    > automatically? Or is this some dream I had?
    >
    > Thanks!


    Well, that depends, if you do: String s = "abc" + "def" + "ghi";

    The compiler turns it into: String s = "abcdefghi";

    However, if you do:

    String s = "";
    for (int i = 0; i < len; i++) { s += "xxx"; }

    Then that is definitely compiled as:

    s = (new StringBuffer()).append(s).append("xxx").toString();

    although it may be optimized to:

    s = (new StringBuffer(s)).append("xxx").toString();

    By the way, this is the reason it's a Bad Thing(tm) to do that sort of
    String concatentation in a loop. Something like the following is
    probably better:

    StringBuffer sb = new StringBuffer(); // or new StringBuffer(len *
    3);?
    for (int i = 0; i < len; i++) { sb.append("xxx"); }
    String s = sb.toString();

    This is covered in the Java 2 documentation: <url:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html />

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Jul 27, 2004
    #3
  4. Darren

    Roedy Green Guest

    On Tue, 27 Jul 2004 10:39:34 +0100, Darren <darren@localhost> wrote or
    quoted :

    >Can someone deny or confirm that the latest sdks convert code like
    >
    >"abc" + "def" + "ghi"
    >
    >into the appropiate StringBuffer code
    >
    >new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
    >
    >automatically? Or is this some dream I had?

    they do better than that. they convert it into one big string
    literal.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 27, 2004
    #4
  5. Darren

    Roedy Green Guest

    On Tue, 27 Jul 2004 17:06:23 GMT, Grant Wagner
    <> wrote or quoted :

    >StringBuffer sb = new StringBuffer(); // or new StringBuffer(len *
    >3);?
    >for (int i = 0; i < len; i++) { sb.append("xxx"); }
    >String s = sb.toString();


    Again most of the time you would never notice the difference, so it is
    best to code for clarity. Sometimes these optimisations are done for
    you by the compiler or runtime.

    However, I make it a habit to use sb for loops or for extensive
    x += work. since that it what other coders would expect.

    For one liners, I use a great string of +, since the compiler will
    generate optimal code for me anyway much more tersely.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 27, 2004
    #5
  6. Darren

    Darren Guest

    Thanks all for the detailed responses - I feel I can become a String
    master now! :)

    Darren wrote:
    > Can someone deny or confirm that the latest sdks convert code like
    >
    > "abc" + "def" + "ghi"
    >
    > into the appropiate StringBuffer code
    >
    > new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
    >
    > automatically? Or is this some dream I had?
    >
    > Thanks!
    >
    Darren, Jul 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    4,783
    walala
    Sep 18, 2003
  2. Mike
    Replies:
    3
    Views:
    1,837
  3. John Galt

    hash for String and StringBuffer

    John Galt, Feb 24, 2004, in forum: Java
    Replies:
    4
    Views:
    542
    Doug Pardee
    Feb 25, 2004
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    239
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    685
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page