- 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 = "w3schools"; object.concatTest1(website1); StringBuilder website2 = new StringBuilder("w3schools"); object.concatTest2(website2); StringBuffer website3 = new StringBuffer("w3schools"); 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 = "w3schools"; 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: w3schools StringBuffer Object: w3schools StringBuffer Object after reverse operation: tniops3w StringBuilder Object: w3schools StringBuilder Object after append operation: w3schools.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("w3schools"); StringBuilder stringBuilder = new StringBuilder("w3schools"); Main main = new Main(); main.stringBufferToString(stringBuffer); main.stringBuilderToString(stringBuilder); } } |
Output
Converted String from StringBuffer: w3schools Converted String from StringBuilder: w3schools |
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("w3schools"); StringBuilder stringBuilder = new StringBuilder("w3schools"); Main main = new Main(); main.stringBufferToStringBuilder(stringBuffer); main.stringBuilderToStringBuffer(stringBuilder); } } |
Output
Converted StringBuilder from StringBuffer: w3schools Converted StringBuffer from StringBuilder: w3schools |
Java interview questions on String Handling
- Why string objects are immutable in java?
- How many ways we can create the string object?
- Why java uses the concept of string literal?
- String vs StringBuffer vs StringBuilder in java
- How to create immutable class in java?
- What is the purpose of toString() method in java?
- Is string a keyword in java?
- Is string a primitive type or derived type?
- What is string constant pool in java?
- What are mutable and immutable objects in java?
- What is string intern in java?
- Can we call string class methods using string literals?