StringBuffer in Java


Prev Tutorial Next Tutorial

StringBuffer Class in Java

It is a predefined class in java.lang package can be used to handle the String, whose object is mutable that means content can be modify.
StringBuffer class is working with thread safe mechanism that means multiple thread are not allowed simultaneously to perform operation of StringBuffer.

StringBuffer class object is mutable that means when we create an object of StringBulder class it can be change.

Example StringBuffer

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java"); 
sb.append("software");
System.out.println(sb);
}
}

Output

javasoftware

Explanation: Here we can changes in the existing object of StringBuffer class so output is javasoftware.

Difference Between String and StringBuffer

StringStringBuffer
The data which enclosed within double quote (" ") is by default treated as String class.The data which enclosed within double quote (" ") is not by default treated as StringBuffer class
String class object is immutableStringBuffer class object is mutable
When we create an object of String class by default no additional character memory space is created.When we create an object of StringBuffer class by default we get 16 additional character memory space.

Similarities Between String and StringBuffer

  • Both of them are belongs to public final. so that they never participates in inheritance that is is-A relationship is not possible but they can always participates in As-A and Uses-A relationship.
  • We can not override the method of String and StringBuffer.

Methods of StringBuffer class

reverse()

reverse(): This method is used to reverse the given string and also the new value is replaced by the old string.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java code");
System.out.println(sb.reverse());
}
}

Output

edoc avaj

insert()

insert(): This method is used to insert either string or character or integer or real constant or boolean value at a specific index value of given string.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("this is my java code");
System.out.println(sb.insert(11, "first "));
}
}

Output

this is my first java code

append()

append(): This method is used to add the new string at the end of original string.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java is easy");
System.out.println(sb.append(" to learn"));
}
}

Output

java is easy to learn

replace()

replace() This method is used to replace any old string with new string based on index value.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("This is my code");
System.out.println(sb.replace(8, 10, "java"));
}
}

Output

This is java code

Explanation: In above example java string is replaced with old string (my) which is available between 8 to 10 index value.

deleteCharAt()

deleteCharAt(): This method is used to delete a character at given index value.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java"); 
System.out.println(sb.deleteCharAt(3));
}
}

Output

jav

delete()

delete(): This method is used to delete string form given string based on index value.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java is easy to learn"); 
StringBuffer s;
s=sb.delete(8, 13);
System.out.println(sb);
}
}

Output

java is to learn

Explanation: In above example string will be deleted which is existing between 8 and 13 index value.

toString()

toString(): This method is used to convert mutable string value into immutable string.

Example

class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java"); 
String s=sb.toString();
System.out.println(s);
s.concat("code");
}
}

Output

java

Prev Tutorial Next Tutorial

Advertisements
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826