Java StringBuilder Class

The String class in immutable, meaning that every time we modify the string object it will creeate a new allocation in memory. This is not desirable when we need to frequently modify the data. Thus we make use of String Builder class. String Builder is a class is mutable by nature. Means that every modification on the object will use same memory location.

StringBuilder Constructors

StringBuilder(): This creates an empty string Builder whose initial capacity of 16.
StringBuilder(String str): This creates a string Builder with the specified string.
StringBuilder(int length): This creates an empty string Builder with the specified capacity as length.

The Sting Builder class supports various string manipulation functionalities. Few of those functionalities are demonstrated bellow.

  1. public class StringManuplator {
  2.  
  3. public static void main(String[] args){
  4. StringBuilder sb1 = new StringBuilder();
  5. StringBuilder sb2 = new StringBuilder("STRING Builder");
  6. StringBuilder sb3 = new StringBuilder(10);
  7.  
  8. sb1.append("Append");
  9. System.out.println(sb1.toString());
  10.  
  11. sb1.insert(2," added ");
  12. System.out.println(sb1.toString());
  13.  
  14. sb1.replace(2,3," replaced ");
  15. System.out.println(sb1.toString());
  16.  
  17. System.out.println(sb1.delete(2,4));
  18.  
  19. System.out.println(sb1.capacity());
  20.  
  21. System.out.println(sb1.reverse().toString());
  22.  
  23. }
  24.  
  25. }

Java StringBuffer Vs StringBuilder

Java StringBuffer Class

String buffer in Java is also mutable like StringBuilder, but they differ only by one functionality. StringBuffer is Synchronised and is Thread Safe, unlike String Builders which are not synchronised. This makes String Buffers more usefull in multi threaded processing. This also is comparatively bit slower than StringBuilder Class. Both of these classes share the similar methods and uses.

StringBuffer Constructors

StringBuffer(): This creates an empty string Builder whose initial capacity of 16.
StringBuffer(String str): This creates a string Builder with the specified string.
StringBuffer(int length): This creates an empty string Builder with the specified capacity as length.

  1. public class StringManuplator {
  2.  
  3. public static void main(String[] args){
  4. StringBuffer sb1 = new StringBuffer();
  5. StringBuffer sb2 = new StringBuffer("STRING Builder");
  6. StringBuffer sb3 = new StringBuffer(10);
  7.  
  8. sb1.append("Append");
  9. System.out.println(sb1.toString());
  10.  
  11. sb1.insert(2," added ");
  12. System.out.println(sb1.toString());
  13.  
  14. sb1.replace(2,3," replaced ");
  15. System.out.println(sb1.toString());
  16.  
  17. System.out.println(sb1.delete(2,4));
  18.  
  19. System.out.println(sb1.capacity());
  20.  
  21. System.out.println(sb1.reverse().toString());
  22.  
  23. }
  24.  
  25. }

This is one of the most asked basic question in java interviews. Hence this topic is covered as part of java tutorials.

LEAVE A REPLY

Please enter your comment!
Please enter your name here