Friday, 3 February 2017

What is StringBuilder Class In Java ?

StringBuilder Class 

All Implemented Interfaces:
Serializable, Appendable, CharSequence

Declaration of StringBuilder:
public final class StringBuilder
extends Object
implements Serializable, CharSequence

This class belongs to java.lang; package. StringBuilder class is same as String Class, but it can be modified(means this class is mutable class). StringBuilder class is not safe in multi-threaded environment ,because StringBuilder class is not synchronized. Due to unsunchronized this class gives better performance than String and StringBuffer

Principle operation of StringBuilder class is append() and insert() methods, which are not available in String class, these methods are overloaded to accept any data type.

append() method add string /sequence of characters at the end of existing sequence. Whereas insert() method add the characters at the specified point.

Constructor Summary of StringBuilder:

Constructor
Description
StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(CharSequence seq)
Constructs a string builder that contains the same characters as the specified CharSequence.
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.

Example of StringBuilder:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class StringBuilderClassExample {
 
 public static void main(String[] args) {
  
  //StringBuilder with default Constructor
  StringBuilder sb = new StringBuilder();
  sb.append("Java");
  sb.append(" ");
  sb.append("Identifiers");
  System.out.println(sb);
  
  //StringBuilder with String in Constructor
  StringBuilder sb1 = new StringBuilder("Java Is Object Oriented");
  System.out.println(sb1);
  
  //StringBuilder with initial capacity in Constructor
  StringBuilder sb2 = new StringBuilder(20);
  sb2.append("I").append(" ").append("Love").append(" ").append("Java");
  System.out.println(sb2);
    
  //Use of append() method - it add data at the ends
  StringBuilder sb3 = new StringBuilder("Java");
  sb3.append("Programming");
  System.out.println(sb3);
  
  //Use of insert() method - it add data at the specified point
  StringBuilder sb4 = new StringBuilder("Java");
  sb4.insert(2, "Thread");
  System.out.println(sb4);
    
 }
}

Output:

Java Identifiers
Java Is Object Oriented
I Love Java
JavaProgramming
JaThreadva




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment