BIG
DATA

JAVA

Java StringBuilder class

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

StringBuilder objects are like String objects, except that they can be modified. Hence Java StringBuilder class is also used to create mutable (modifiable) string object. StringBuilder is same as StringBuffer except for one important difference. StringBuilder is not synchronized, which means it is not thread safe. At any point, the length and content of the sequence can be changed through method invocations.

StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread. Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Constructors of StringBuilder class

  • StringBuilder ( ) : Constructs a string builder with no characters in it and an initial capacity of 16 characters.
  • StringBuilder ( int capacity ) : Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
  • StringBuilder ( String str ) : Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.

Important methods of StringBuilder class

append()

The append() method concatenates the given argument(string representation) to the end of the invoking StringBuilder object. StringBuilder class has several overloaded append() method. Few are:

  • StringBuilder append(String str)
  • StringBuilder append(int n)
  • StringBuilder append(Object obj)
StringBuilder strBuilder = new StringBuilder("Core");
strBuilder.append("JavaGuru");
System.out.println(strBuilder);
strBuilder.append(101);
System.out.println(strBuilder);

Output:

CoreJavaGuru
CoreJavaGuru101
Read more »

insert()

The insert() method inserts the given argument(string representation) into the invoking StringBuilder object at the given position.

StringBuilder  strBuilder=new StringBuilder ("Core");  
strBuilder.insert(1,"Java");
System.out.println(strBuilder);

Output:

CJavaore

replace()

The replace() method replaces the string from specified start index to the end index.

StringBuilder strBuilder=new StringBuilder("Core");  
strBuilder.replace( 2, 4, "Java");
System.out.println(strBuilder);

Output:

CoJava

reverse()

This method reverses the characters within a StringBuilder object.

StringBuilder strBuilder=new StringBuilder("Core");  
strBuilder.reverse();
System.out.println(strBuilder);

Output:

eroC

delete()

The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.

StringBuilder strBuilder=new StringBuilder("Core");  
strBuilder.delete( 2, 4);
System.out.println(strBuilder);

Output:

Co

capacity()

The capacity() method returns the current capacity of StringBuilder object. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

StringBuilder strBuilder=new StringBuilder();  
System.out.println(strBuilder.capacity());
strBuilder.append("1234");
System.out.println(strBuilder.capacity());
strBuilder.append("123456789112");
System.out.println(strBuilder.capacity());
strBuilder.append("1");
System.out.println(strBuilder.capacity()); //(oldcapacity*2)+2  

StringBuilder strBuilder2=new StringBuilder("1234");  
System.out.println(strBuilder2.capacity());

Output:

16
16
16
34
20

Observe how the capacity changes.

ensureCapacity()

The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.

If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

StringBuilder strBuilder=new StringBuilder("Core");  
strBuilder.ensureCapacity(10);
System.out.println(strBuilder.capacity());

StringBuilder strBuilder2=new StringBuilder("Core");  
strBuilder2.ensureCapacity(30);
System.out.println(strBuilder2.capacity());

Output:

20
42

Difference between StringBuffer and StringBuilder

Some of differences between StringBuffer and StringBuilder are given below:

StringBuffer StringBuilder
StringBuffer is synchronized i.e. thread safe. StringBuilder is non-synchronized i.e. not thread safe.
StringBuffer is less efficient and slower than StringBuilder as StringBuffer is synchronized. StringBuilder is more efficient and faster than StringBuffer.
StringBuffer is old, its there in JDK from very first release. StringBuilder is introduced much later in release of JDK 1.5

String, StringBuffer and StringBuilder - Which one to use ?

  • If your string is not going to change use a String class because a String object is immutable.
  • If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
  • If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.