StringBuffer replacing String for memory Issues Help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VigneshMohan
    New Member
    • Apr 2009
    • 10

    StringBuffer replacing String for memory Issues Help!!

    Hi All

    I wanted to replace the String Concatenation using " + " with a string buffer to eliminate performance hogs.

    I have a string

    String str = "Testing "+ firstStrVariabl e + "NextTest" + secondStringVar iable + "ThirdTest" ;

    Is it worth replacing the String in this case with StringBuffer and StringBuffer.Ap pend

    Any Suggestions pl post it
    -

    Thanks
    Vignesh.M
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by VigneshMohan
    Hi All

    I wanted to replace the String Concatenation using " + " with a string buffer to eliminate performance hogs.

    I have a string

    String str = "Testing "+ firstStrVariabl e + "NextTest" + secondStringVar iable + "ThirdTest" ;

    Is it worth replacing the String in this case with StringBuffer and StringBuffer.Ap pend

    Any Suggestions pl post it
    -

    Thanks
    Vignesh.M
    If at least one of the operands of the + operator is a String the compiler plays a few tricks: it uses a StringBuffer (or StringBuilder) to catenate the Strings. If you don't want to add Strings a million times you can safely use the + operator. Otherwise use the sb.append() method repeatedly.

    kind regards,

    Jos

    Comment

    • VigneshMohan
      New Member
      • Apr 2009
      • 10

      #3
      Originally posted by JosAH
      If at least one of the operands of the + operator is a String the compiler plays a few tricks: it uses a StringBuffer (or StringBuilder) to catenate the Strings. If you don't want to add Strings a million times you can safely use the + operator. Otherwise use the sb.append() method repeatedly.

      kind regards,

      Jos
      Hi
      JosAH

      But appending a string using " + " will create a new string object right

      Which all cases will create a new string object with the use of + ?

      Will all the different arise only when we use threads?

      In other cases are these all same. ?
      -
      Thanks
      Vignesh.M

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by VigneshMohan
        Hi
        JosAH

        But appending a string using " + " will create a new string object right

        Which all cases will create a new string object with the use of + ?

        Will all the different arise only when we use threads?

        In other cases are these all same. ?
        -
        Thanks
        Vignesh.M
        If a, b and c are Strings and you want to 'add' them, the compiler will generate the following:

        Code:
        String a, b, c;
        String result= new StringBuffer(new StringBuffer(a).append(b).toString()).append(c).toString();
        Manually you could do this with just one StringBuffer:

        Code:
        String a, b, c;
        String result= new StringBuffer(a).append(b).append(c).toString();
        Note that this all has nothing to do with Threads.

        kind regards,

        Jos

        Comment

        • VigneshMohan
          New Member
          • Apr 2009
          • 10

          #5
          Originally posted by JosAH
          If a, b and c are Strings and you want to 'add' them, the compiler will generate the following:

          Code:
          String a, b, c;
          String result= new StringBuffer(new StringBuffer(a).append(b).toString()).append(c).toString();
          Manually you could do this with just one StringBuffer:

          Code:
          String a, b, c;
          String result= new StringBuffer(a).append(b).append(c).toString();



          Note that this all has nothing to do with Threads.

          kind regards,

          Jos


          Great ...

          I need one more clarification

          If I give like this

          Code:
          String aStringVariable = "welcome";
          String test = "sample"+aStringVariable+"sample2";

          What will the compiler do...?
          If we give constants how the compiler interprets internally?

          One more Doubt.

          StringBuffer is Synchronous and StringBuilder is Unsynchronous
          Could you explain this?

          -
          Thanks

          Vignesh.M

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by VigneshMohan
            What will the compiler do...?
            If we give constants how the compiler interprets internally?

            One more Doubt.

            StringBuffer is Synchronous and StringBuilder is Unsynchronous
            Could you explain this?
            If the compiler sees a + operator with String operands it generates a StringBuffer given the left String operand; appends the right String operand and retrieves the (new) String by invoking the toString() method on that StringBuffer. If it sees another + operator it'll simply do the same (also see my previous reply).

            If you use one Thread only for your StringBuffer operations you can better use a StringBuilder because the latter doesn't synchronize its methods (compare with Vector and ArrayList).

            kind regards,

            Jos

            Comment

            Working...