Advertisement



< Prev
Next >



StringBuilder Class









When StringBuilder class is used?







Constructors of StringBuilder class


public StringBuilder()
Creates a StringBuilder object with initial capacity of 16 characters.

public StringBuilder(String str)
Creates a StringBuilder object using a String str.

public StringBuilder(int capacity)
Creates a StringBuilder object with a defined capacity of characters.




Creating an empty StringBuilder object


// StringBuilder Class Example

class StringBuilderEx
{
public static void main(String... ar)
{
StringBuilder sb= new StringBuilder(); //Creating an empty StringBuilder object.
System.out.println("Original StringBuilder value : "+ sb);
sb.append("Blue");
sb.append("Budgie");
System.out.println("Modified StringBuilder value : "+ sb);
}
}


Output is :


Original StringBuilder value is : 
Modified StringBuilder value is : BlueBudgie


Program Analysis





Advertisement




Creating and initializing a StringBuilder object using a String


// StringBuilder Class Example

class StringBuilderEx
{
public static void main(String... ar)
{
StringBuilder sb= new StringBuilder("Blue");

System.out.println("Original StringBuilder value : "+ sb);
sb.append("Sky");
System.out.println("Modified StringBuilder value : "+ sb);
}
}


Output is :


Original StringBuilder value : Blue
Modified StringBuilder value : BlueSky


Program Analysis







Reversing the string created by StringBuilder class


class A
{
public static void main(String... ar()
{
StringBuilder sb= new StringBuilder("Hello"); 
System.out.println("Original StringBuilder value : "+ sb); 
sb.reverse(); //reversing the original string
System.out.println("Modified reverse StringBuilder value : "+ sb);
}


Output is :


Original StringBuffer string value : Hello
Modified reverse StringBuilder value : olleh




Please share this article -




< Prev
Next >
< String v/s StringBuffer
String v/s StringBuilder >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement