Course Content
Java RMI
0/1
Java Inner class
0/1
Java File class
0/1
Layout Managers
0/1
Java Programming Tutorial
About Lesson

Java StringBuilder class

StringBuilder is identical to StringBuffer except for one important difference that it is not synchronized, which means it is not thread safe.

StringBuilder also used for creating string object that is mutable and non synchronized. The StringBuilder class provides no guarantee of synchronization. StringBuffer and StringBuilder both are mutable but if synchronization is not required then it is recommend to use StringBuilder class.

This class is located into java.lang package and signature of the class is as:

public final class StringBuilder
extends Object
implements Serializable, CharSequence
 

StringBuilder Constructors

  1. StringBuilder (): creates an empty StringBuilder and reserves room for 16 characters.
  2. StringBuilder (int size): create an empty string and takes an integer argument to set capacity of the buffer.
  3. StringBuilder (String str): create a StringBuilder object and initialize it with string str.
  4. StringBuilder (CharSequence seq): It creates stringbuilder object by using CharSequence object.

Creating a StringBuilder Class

Lets use StringBuilder class to create string object and check its mutability also.

 
public class Demo {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("onlineexam");
    System.out.println(sb);
    // modifying object
    sb.append("guide.com");
    System.out.println(sb);
  }
}
 

onlineexam onlineexamguide.com

Difference between StringBuffer and StringBuilder class

StringBuffer class StringBuilder class
StringBuffer is synchronized. StringBuilder is not synchronized.
Because of synchronisation, StringBuffer operation is slower than StringBuilder. StringBuilder operates faster.
StringBuffer is thread-safe StringBuilder is not thread-safe
StringBuffer is less efficient as compare to StringBuilder StringBuilder is more efficient as compared to StringBuffer.
Its storage area is in the heap Its storage area is the stack
It is mutable It is mutable
Methods are synchronized Methods are not synchronized
It is alternative of string class It is more flexible as compared to the string class
Introduced in Java 1.0 Introduced in Java 1.5
Its performance is moderate Its performance is very high
It consumes more memory It consumes less memory

Example of StringBuffer class


public class BufferDemo{
    public static void main(String[] args){  
StringBufferobj=new StringBuffer("Welcome to ");  
obj.append("onlineexamguide.com");  
System.out.println(obj);  
    }  
}  
  
 

Welcome to onlineexamguide.com

Example of Stringbuilder Class


public class BuilderDemo{
    public static void main(String[] args){  
StringBuilderobj=new StringBuilder("Welcome to ");  
obj.append("onlineexamguide.com");  
System.out.println(obj);  
    }  
}   
 

Welcome to onlineexamguide.com

StringBuilder Methods

StringBuilder class has various methods to handle string object, such as append, insert, replace, reverse etc. Lets see usage of these with the help of examples.

Example of StringBuilder append string

In this example, we are appending a new string using appen() method to the existing string object.

public class Demo {

  public static void main(String[] args) {

    StringBuilder sb = new StringBuilder("onlineexam");
    System.out.println(sb);
    // appending object
    sb.append("guide.com");
    System.out.println(sb);

  }
}
 

onlineexam onlineexamguide.com

StringBuilder Replace Method

It is used to replace a substring from the string object. This method takes three arguments, first is start index, second is last index and third is substring to be replaced.


public class Demo {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Java is a 
programming language"
); System.out.println(sb); // replacing object sb.replace( 10, 21, "computer"); System.out.println(sb); } }
 

Java is a programming language Java is a computer language

StringBuilder Reverse Method

It is used to reverse the string object. It completely reverses the string from start to end characters. See the below example.

public class Demo {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("Java 
stringbuilder"
); System.out.println(sb); // reverse object sb.reverse(); System.out.println(sb); } }
 

Java stringbuilder redliubgnirts avaJ

error: Content is protected !!