Course Content

Course Content

Description

StringBuffer and StringBuilder Classes in Java

In the previous video of our Core Java course we have seen about Object Wrapper class in Java programming language. Now in this video we are going learn about what are StringBuffer class in Java and StringBuilder class in Java programming.

The StringBuffer class in Java and StringBuilder class in Java are used when there is a necessity to make a lot of modifications to strings of characters.

The main difference between StringBuffer and StringBuilder classes is that methods are not thread safe and not synchronised.

StringBuffer class in Java is used when we have to make a lot of modifications to our string. The StringBuilder class in Java is same as StringBuffer class except that is non-synchronized.

StringBuffer defines four different constructors,
1. StringBuffer()
2. StringBuffer(int size)
3. StringBuffer(String str)
4. StringBuffer(charSequence []ch)

Methods of StringBuffer class are,
1. Append() : this method will concatenate the string representation of any type of data to the end of the invoking StringBuffer object.
2. StringBuffer append(String str)
3. StringBuffer append(int n)
4. StringBuffer append(Object obj)

Now we will see one example for Append method in Java programming.
class stringtest
{
public static void main (String args [])
{
StringBuffer str = new StringBuffer ("roll number is:->");
str.append(123);
System.out.println(str);
}
}
Output :
roll number is:->123

The insert() method in Java inserts the given string with the string at the given position.
Example :
class insert
{
public static void main (String args [])
{
StringBuffer sb=new StringBuffer ("Hello ");
//sb.insert(1,"Java");
sb.insert (5,"Java");
System.out.println(sb);
}
}
Output :
HelloJava

The replace() method in Java replaces the given string from the specified beginIndex and endIndex.
Example :
class replace
{
public static void main(String args [])
{
StringBuffer sb=new StringBuffer("Hello");
sb.replace (1,3,"Java"); System.out.println(sb);
}
}
Output :
HJavalo

The delete() method of StringBuffer class in Java deletes the string from the specified beginIndex and endIndex.
Example :
class delete
{
public static void main(String args [])
{
StringBuffer sb=new StringBuffer ("Hello");
sb.delete (1,3);
System.out.println(sb) ;
}
}
Output :
Hlo

The reverse() method of StringBuilder class in Java reverses the current string.
Example :
class stringmethod
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}
Output :
olleH

The capacity() method in Java returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by '(oldcapacity*2)+2'.
Example :
class capacity
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer ();
System.out.println (sb.capacity ()); sb.append ("Hello");
System.out.println (sb. capacity ());
sb.append ("java is my favourite language");
System.out.println (sb.capacity ()); //now (16*2)+234 i.e (oldcapacity*2)+2
}
}
Output :
16
16
34

So, we have learnt that what are StringBuffer class in Java and what are StringBuilder class in Java programming with Methods of StringBuffer class and also four different constructors defined by StringBuffer class.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere