String Builder

A String Builder is like a String but can be modified. String has the drawback that once created it can not be modified. To overcome that problem String Buffer and String builder can be used.

Difference between String Buffer and String Builder

‘StringBuffer’ is safe for use by multiple threads(Thread Safe). The methods are synchronized. String Builder is not thread safe.

Where possible, it is recommended that String Builder should be used in preference to StringBuffer as it will be faster in most of the cases.

Important Methods of String Buffer

append()
Insert()
Length()

Cheatsheet

  • Difference between StringBuilder and String Buffer is that StringBuilder method is not synchronized.
  • String Builder is mutable which is opposite to String which is Immutable.
  • StringBuilder’s equals method is not overriden, Hence it doesn’t compare the value of StringBuilder. As it does in case of String.

9 Comments String Builder

  1. priya

    StringBuffer is synchronized as well as Thread safe so can be used in multithreading. StringBuilder is not synchronized as well as thread safe. Many thanks for sharing this.

    Reply
  2. Mayur kohli

    hey,
    Its good and informative article , you have covered basic difference here between string builder and string buffer, it could be more precise and and include more points. Helpful article.
    Thanks a lot for sharing!

    Reply
  3. HEMANTH BOLLAMREDDI

    CODE :
    package LearningPackage2;
    public class Class21 {

    public static void main(String[] args) {
    String s = “HI”;
    s = s + ” JBT”;
    System.out.println(s);
    }

    }
    OUTPUT :
    HI JBT
    “String has drawback that once created it can not be modified” . I THINK ITS WRONG. PLEASE CHECK ONCE MORE

    Reply
  4. Mahitha R

    Hi Vivek,

    All your tutorials are sooo good..But it will be helpful only if next button is visible to navigate..Everything i read is like half cooked

    Reply
    1. Jaimin

      Hello,
      Thanks for your tutorial but, your tutorial for String,StringBuffer and StringBuilder classes are good but it sholud be with complete information. It looks like you provided very less details about it. Looks as Half cooked..!

      Reply
      1. Vinay

        Simple example for string,string buffer and string builder:-
        public class StringBufferStringBuilder {

        // Concatenates to String
        public static void concat1(String s1)
        {
        s1 = s1 + “John”;
        }

        // Concatenates to StringBuilder
        public static void concat2(StringBuilder s2)
        {
        s2.append(“John”);
        }

        // Concatenates to StringBuffer
        public static void concat3(StringBuffer s3)
        {
        s3.append(“John”);
        }

        public static void main(String[] args)
        {
        String s1 = “Hello”;
        concat1(s1); // s1 is not changed
        System.out.println(“String: ” + s1);

        StringBuilder s2 = new StringBuilder(“Geeks”);
        concat2(s2); // s2 is changed
        System.out.println(“StringBuilder: ” + s2);

        StringBuffer s3 = new StringBuffer(“Geeks”);
        concat3(s3); // s3 is changed
        System.out.println(“StringBuffer: ” + s3);
        }
        }

        Output:-
        Hello
        Hello John
        Hello John

        Note:- String is immutable,Value cannot be modified.

        Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.