String Builder

A String Builder is like a String, but can be modified. String has 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’ are 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 StringBuilders method are 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.
Series Navigation<< InheritanceJava String Tutorial >>
By | 2017-07-09T12:14:57+00:00 April 21st, 2012|Core Java|6 Comments

About the Author:

6 Comments

  1. Mahitha R April 17, 2014 at 7:05 am - Reply

    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

    • Jaimin April 20, 2016 at 12:17 pm - Reply

      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..!

      • J Singh April 21, 2016 at 3:20 am - Reply

        I agree. We will work all these article to make it complete.

      • Vinay May 9, 2016 at 9:44 am - Reply

        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.

  2. HEMANTH BOLLAMREDDI July 18, 2016 at 8:11 am - Reply

    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

    • J Singh September 3, 2016 at 10:17 am - Reply

      Statement is correct. You can not change the string. But here you are actually creating a new String and not updating the existing String.

      Regards

Leave A Comment