How to use a StringBuffer



  •  One of our developers, who has since quit her job as an IT specialist to pursue a career in music, once checked in the following Java code:

        for(int i=0; i < someLimit; i++) {
            ...
            stringVar = stringVar + some_other_string;
            ...
        }

    Another co-worker pointed out to her that concatenating strings in a loop wasn't a good idea, especially in a method that was called very often, and that she should use a StringBuffer instead.
    She accepted his suggestion and rewrote the code:

        for(int i=0; i < someLimit; i++) {
            ...
            stringVar = stringVar + new StringBuffer(some_other_string);
            ...
        }



  • Reminds me of my two best friends back in high school. Friend A says to friend B, "Hey, spell 'cold' with a 'K'!" Friend B says "C-O-L-K!"


  • SockDev

    @mott555 said:

    Reminds me of my two best friends back in high school. Friend A says to friend B, "Hey, spell 'cold' with a 'K'!" Friend B says "C-O-L-K!"

    To be fair, friend B didn't get it wrong :)

     



  • Shit, is it even possible to do that?

    ...

    Yes, you can concatenate a String with a StringBuffer/StringBuilder. Crazy shit.


  • Discourse touched me in a no-no place

    @ronin said:

    who has since quit her job as an IT specialist to pursue a career in music

    Wow, sometimes people do make good choices!



  • You can concatenate a String with any object, and Java will automatically call toString() on it. Concatenate a string with a HashMap<String, String> if you like.



  • Doesn't javac convert string concatenations like that into a StringBuffer anyway? I recall reading something like that last time I cared about java.



  • @ronin said:

    who has since quit her job as an IT specialist to pursue a career in music
     

    You mention that as if musical ability is mutually exclusive to programming ability or analytical thought.

    If I'm not mistaken, several of our users ("wtfuckies" as I like to call 'em) (including myself) have some proficiency with a musical instrument, and my musician friend is an adept Arduinist.

    I don't know if Arduinist is a word, but I am making it so either way.

     

    Actual response:

    There, all fixed!



  • @pkmnfrk said:

    Doesn't javac convert string concatenations like that into a StringBuffer anyway? I recall reading something like that last time I cared about java.
     

    I hope it does. What's the point if the compiler doesn't make your life easier? If not for high level languages and compilers, we'd still be constructing CPU instructions by hand.



  • @dhromed said:

    @pkmnfrk said:

    Doesn't javac convert string concatenations like that into a StringBuffer anyway? I recall reading something like that last time I cared about java.

    I hope it does. What's the point if the compiler doesn't make your life easier? If not for high level languages and compilers, we'd still be constructing CPU instructions by hand.

    Using only a magnetized needle and a steady hand, directly on an exposed hard disk platter.



  • @pkmnfrk said:

    Doesn't javac convert string concatenations like that into a StringBuffer anyway? I recall reading something like that last time I cared about java.
     

    Yes, it does.

    It didn't by the turn of the century, but was fixed by then. The entire thing is a nest of WTFs, but after a couple of decades Sun made it act at the way you expect (just don't look inside). I was left wondering at what year that co-worker thinks he's living.



  • @Mcoder said:

    Yes, it does.

    Only for inline string concatenations. If you're using loops you still need to explicitly use a StringBuilder, even with the Java 7 compiler.



  •  It is worth mentioning if anyone care that stringBuffer.toString() copies the entire string.



  • @Mcoder said:

    @pkmnfrk said:

    Doesn't javac convert string concatenations like that into a StringBuffer anyway? I recall reading something like that last time I cared about java.
     

    Yes, it does.

    It didn't by the turn of the century, but was fixed by then. The entire thing is a nest of WTFs, but after a couple of decades Sun made it act at the way you expect (just don't look inside). I was left wondering at what year that co-worker thinks he's living.

    The thing is this: The code

        for(int i=0; i < someLimit; i++) {
            ...
            stringVar = stringVar + some_other_string;
            ...
        }

    is compiled into

        for(int i=0; i < someLimit; i++) {
            ...
            StringBuilder temp = new StringBuilder();
            temp.append(stringVar);
            temp.append(some_other_string);
            stringVar = temp.toString();
            ...
        }

    which is fine in its own, but since it is inside a loop, it can be tuned a lot like this, avoiding unneccessary creation of StringBuilder objects and toString() method calls:

        StringBuilder temp = new StringBuilder();
        for(int i=0; i < someLimit; i++) {
            ...
            temp.append(some_other_string);
            ...
        }
        stringVar = temp.toString();


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.