String vs StringBuffer vs StringBuilder in java

  • String objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
  • StringBuffer and StringBuilder are same only difference is that StringBuilder is not synchronized whereas StringBuffer is. As StringBuilder is not synchronized, it is not thread safe but faster than StringBuffer.
  • Use String, for a string which will be constant through out the application.
    Use StringBuffer, for a string which can change in multi-threaded applications.
    Use StringBuilder, for a string which can change in single-threaded applications.

Example of String, StringBuffer and StringBuilder

public class Main 
{ 
    long startTime = 0l;
    long endTime = 0l;
    long timeDiff = 0l;
    // Concatenate operation using String 
    public void concatTest1(String website) 
    { 
        startTime = System.nanoTime();
        website = website + ".com";
        endTime = System.nanoTime();
        timeDiff = endTime - startTime;
        System.out.println("Concat time by String: " + timeDiff);
    } 
 
    // Concatenate operation using StringBuilder 
    public void concatTest2(StringBuilder website) 
    {  
        startTime = System.nanoTime();
        website.append(".com"); 
        endTime = System.nanoTime();
        timeDiff = endTime - startTime;
        System.out.println("Concat time by StringBuilder: " + timeDiff);
    } 
 
    // Concatenate operation using StringBuffer 
    public void concatTest3(StringBuffer website) 
    { 
        startTime = System.nanoTime();
        website.append(".com");
        endTime = System.nanoTime();
        timeDiff = endTime - startTime;
        System.out.println("Concat time by StringBuffer: " + timeDiff);
    } 
 
    public static void main(String[] args) 
    { 
        Main object = new Main();
        String website1 = "w3spoint"; 
        object.concatTest1(website1);
 
        StringBuilder website2 = new StringBuilder("w3spoint"); 
        object.concatTest2(website2);
 
        StringBuffer website3 = new StringBuffer("w3spoint"); 
        object.concatTest3(website3); 
    } 
}

Output

Concat time by String: 43970
Concat time by StringBuilder: 760
Concat time by StringBuffer: 3470

Conversion between types of strings in Java

String to StringBuffer and StringBuilder

Example

public class Main 
{ 
 
    public static void main(String[] args) 
    { 
        String stringObj = "w3spoint"; 
 
        System.out.println("String Object: " + stringObj);  
 
        // String to StringBuffer 
        StringBuffer stringBuffer = new StringBuffer(stringObj); 
        System.out.println("StringBuffer Object: " + stringBuffer);
        stringBuffer.reverse();
        System.out.println("StringBuffer Object after reverse operation: " + stringBuffer);
 
        //String to StringBuilder 
        StringBuilder stringBuilder = new StringBuilder(stringObj); 
        System.out.println("StringBuilder Object: " + stringBuilder);
        stringBuilder.append(".com"); 
        System.out.println("StringBuilder Object after append operation: " + stringBuilder);
    } 
 
}

Output

String Object: w3spoint
StringBuffer Object: w3spoint
StringBuffer Object after reverse operation: tniops3w 
StringBuilder Object: w3spoint
StringBuilder Object after append operation: w3spoint.com

StringBuffer and StringBuilder to String

Example

public class Main 
{ 
    void stringBufferToString(StringBuffer stringBuffer){
        String stringObj = stringBuffer.toString(); 
        System.out.println("Converted String from StringBuffer: " + stringObj); 
    }
 
    void stringBuilderToString(StringBuilder stringBuilder){
        String stringObj = stringBuilder.toString(); 
        System.out.println("Converted String from StringBuilder: " + stringObj); 
    }
 
    public static void main(String[] args) 
    {   
        StringBuffer stringBuffer = new StringBuffer("w3spoint"); 
        StringBuilder stringBuilder = new StringBuilder("w3spoint");
        Main main = new Main();
        main.stringBufferToString(stringBuffer);
        main.stringBuilderToString(stringBuilder);
    } 
 
}

Output

Converted String from StringBuffer: w3spoint
Converted String from StringBuilder: w3spoint

StringBuffer to StringBuilder or StringBuilder to StringBuffer

Example

public class Main 
{ 
    void stringBufferToStringBuilder(StringBuffer stringBuffer){
        String stringObj = stringBuffer.toString(); 
        StringBuilder stringBuilder = new StringBuilder(stringObj);
        System.out.println("Converted StringBuilder from StringBuffer: " + stringObj); 
    }
 
    void stringBuilderToStringBuffer(StringBuilder stringBuilder){
        String stringObj = stringBuilder.toString(); 
        StringBuffer stringBuffer = new StringBuffer(stringObj); 
        System.out.println("Converted StringBuffer from StringBuilder: " + stringObj); 
    }
 
    public static void main(String[] args) 
    {   
        StringBuffer stringBuffer = new StringBuffer("w3spoint"); 
        StringBuilder stringBuilder = new StringBuilder("w3spoint");
        Main main = new Main();
        main.stringBufferToStringBuilder(stringBuffer);
        main.stringBuilderToStringBuffer(stringBuilder);
    } 
 
}

Output

Converted StringBuilder from StringBuffer: w3spoint 
Converted StringBuffer from StringBuilder: w3spoint

Java interview questions on String Handling

Please Share