What is StringBuffer in Java?

What is StringBuffer in Java?

6 mins read808 Views Comment
clickHere
Updated on Mar 17, 2023 14:57 IST

The below article goes through the explanation and implementation of the mutable string called StringBuffer in Java along with examples.

2022_06_stringbuffer-java.jpg

The Java StringBuffer class is utilized to represent characters that can be altered. Java StringBuffer is faster than String is the major performance difference when executing simple concatenations. 

Explore- Courses on Java Programming.

Contents

What is StringBuffer in Java?

Just like Strings in Java, we have its sibling called StringBuffer in Java. The major difference between String and StringBuffer classes is that String creates immutable string objects (which means they can’t be altered).

On the other hand, the StringBuffer class created mutable objects, i.e., one can alter the state of the same object again and again. Hence, two or more threads can work upon the same object simultaneously. StringBuffer is thread-safe, as all its methods are synchronized.

How to create a StringBuffer object?

Some of the StringBuffer constructor forms to create an object:

Constructor Description
StringBuffer() Creates a StringBuffer with no characters and an initial capacity of 16 characters.
StringBuffer(int capacity) It creates a string buffer with no characters and an initial capacity to the specified capacity passed as an argument to the method.
StringBuffer(String str) Creates a StringBuffer with contents as that of the string object passed.

Java StringBuffer class methods

Following are a few of the methods under the StringBuffer class for StringBuffer manipulation:

length()

This method returns the number of characters in the StringBuffer object.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #666666;font-style: italic">\n //StringBuffer object with characters \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Length of the stringbuffer string: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n length \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output1

Read: Loops in Java

capacity()

The capacity method in StringBuffer returns the number of characters the StringBuffer can hold.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Capacity of the empty stringbuffer string: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n capacity \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n <span style="color: #666666;font-style: italic">\n //returns 16 \n
ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Capacity of the stringbuffer string: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n capacity \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n <span style="color: #666666;font-style: italic">\n //adds 16 + 15 \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output2

append()

This method appends or attaches at the end, the string representation of the argument passed.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n double l \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 2.0 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Appended text: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n append \n <span style="color: #009900">\n (l \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output3

charAt()

This method returns the characters at the given index from the StringBuffer object.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Show character at index 4 in stringbuffer: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n charAt \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 4 \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output4

For more, Read: OOPs Concepts in Java.

insert()

Insert method insert a given argument at the given index.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "This is a value " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Using insert at position 10: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n insert \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 10, \n <span style="color: #000066;font-weight: bold">\n true \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output5

Read: Implementing Array in Java

delete() and deleteCharAt()

The delete() method deletes characters from a starting index till the end index – 1. Whereas, deleteCharAt() method removes only the character specified in the argument.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Using deleteChatAt: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n deleteCharAt \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 3 \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Using delete: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n delete \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 11, \n <span style="color: #cc66cc">\n 15 \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

Read: Access Modifiers in Java

replace()

This method replaces characters in a StringBuffer object with another string beginning from a start index till end index – 1.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Replaced Text: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n replace \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 7, \n <span style="color: #cc66cc">\n 15, \n <span style="color: #0000ff">\n "Recruitment" \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #cc66cc"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

Also Read: Difference between JDK, JRE, and JVM

reverse()

The reverse() method is used to reverse the contents of a StringBuffer object.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Reversed Text: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n reverse \n <span style="color: #009900">\n ( \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

Also, Read: Java Operators Explained

substring()

This method returns a substring or a subsequence of the given StringBuffer object. It takes as a parameter the starting index and the end index from which the substring has to be extracted. The extract starts from the start index and runs till the index – 1.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "SubString Text: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n substring \n <span style="color: #009900">\n ( \n <span style="color: #cc66cc">\n 8, \n <span style="color: #cc66cc">\n 14 \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #cc66cc"> \n </span style="color: #cc66cc"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

Read: Exception Handling in Java.

indexOf() and lastIndexOf()

The IndexOf() returns the index representing the first occurrence of a string or a substring. Whereas, lastIndexOf() return index of last occurrence.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class StringBuffer_Class \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n StringBuffer ob1 \n <span style="color: #339933">\n = \n <span style="color: #000000;font-weight: bold">\n new \n <span style="color: #003399">\n StringBuffer \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Shiksha Online" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Text: " \n <span style="color: #339933">\n +ob1 \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "first occurence: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n indexOf \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "i" \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "last occurence: " \n <span style="color: #339933">\n +ob1. \n <span style="color: #006633">\n lastIndexOf \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "i" \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

Read: Reverse a String in Java

Super Keyword in Java
Super Keyword in Java
This Java Tutorial article covers one of the most important concepts in Inheritance which is Super Keyword in Java. Here, we’ll go through the uses of super keyword along with...read more
Jump Statements in Java
Jump Statements in Java
In this article, we’ll cover the Jump Statements in Java, such as break, continue and return, along with examples and explanations.
Conditional Statements in Java
Conditional Statements in Java
The below article goes through the Conditional Statements in Java. It covers if, if-else, nested if-else, and switch statements with examples. 

Difference between String and StringBuffer

String StringBuffer
Immutable Mutable
Slow while doing concatenation as it created another string object. Faster than String in terms of concatenation.
Less efficient More efficient
Consumes more memory space. Consumes less memory space as compared to a string.
It uses a string constant pool to store the values. Prefers heap memory to store the objects

For more, Read: 8 Most Important Data Structures a Programmer Must Know 

Conclusion

I hope you enjoyed reading the above article. If you have any queries, feel free to reach us at the link below. Happy Learning!

To learn more, Go for Best Courses on Java Programming.

Data Type in Java | Features of Java Programming | Jump Statement in Java | OOPS in Java Java Interview Questions | Python vs Java | Conditional Statement in Java | Data Abstraction in Java | Super Keyword in Java | Method Overloading in Java | Difference between Java and Javascript | Constructors in Java | Method Overriding in Java Data Structure and Algorithm in Java | Abstract class in Java | Loops in Java

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments