Home Java Programs OOPs String Exception Multithreading
Java StringBuffer Class
Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer
class in Java is the same as String class except it is mutable i.e. it can be changed.
Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it
simultaneously. So it is safe and will result in an order.
Important Constructors of StringBuffer Class
Constructor Description
StringBuffer() It creates an empty String buffer with the initial capacity of 16.
StringBuffer(String str) It creates a String buffer with the specified string..
StringBuffer(int It creates an empty String buffer with the specified capacity as
capacity) length.
Important methods of StringBuffer class
Modifier Method Description
and Type
public append(String s) It is used to append the specified string with this string.
synchronized The append() method is overloaded like append(char),
StringBuffer append(boolean), append(int), append(float),
append(double) etc.
public insert(int offset, It is used to insert the specified string with this string
synchronized String s) at the specified position. The insert() method is
StringBuffer overloaded like insert(int, char), insert(int, boolean),
insert(int, int), insert(int, float), insert(int, double) etc.
public replace(int It is used to replace the string from specified startIndex
synchronized startIndex, int and endIndex.
StringBuffer endIndex, String
str)
public delete(int It is used to delete the string from specified startIndex
synchronized startIndex, int and endIndex.
StringBuffer endIndex)
public reverse() is used to reverse the string.
synchronized
StringBuffer
public int capacity() It is used to return the current capacity.
public void ensureCapacity(int It is used to ensure the capacity at least equal to the
minimumCapacity) given minimum.
public char charAt(int index) It is used to return the character at the specified
position.
public int length() It is used to return the length of the string i.e. total
number of characters.
public String substring(int It is used to return the substring from the specified
beginIndex) beginIndex.
public String substring(int It is used to return the substring from the specified
beginIndex, int beginIndex and endIndex.
endIndex)
What is a mutable String?
A String that can be modified or changed is known as mutable String. StringBuffer and
StringBuilder classes are used for creating mutable strings.
1) StringBuffer Class append() Method
The append() method concatenates the given argument with this String.
StringBufferExample.java
class
StringBufferExample{
bli i id
Output:
Hello Java
2) StringBuffer insert() Method
The insert() method inserts the given String with this string at the given position.
StringBufferExample2.java
class
StringBufferExample2{
bli i id
Output:
HJavaello
3) StringBuffer replace() Method
The replace() method replaces the given String from the specified beginIndex and endIndex.
StringBufferExample3.java
class
StringBufferExample3{
bli i id
Output:
HJavalo
4) StringBuffer delete() Method
The delete() method of the StringBuffer class deletes the String from the specified beginIndex
to endIndex.
StringBufferExample4.java
class
StringBufferExample4{
bli i id
Output:
Hlo
5) StringBuffer reverse() Method
The reverse() method of the StringBuilder class reverses the current String.
StringBufferExample5.java
class
StringBufferExample5{
bli i id
Output:
olleH
6) StringBuffer capacity() Method
The capacity() method of the StringBuffer class 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. For example if your current capacity
is 16, it will be (16*2)+2=34.
StringBufferExample6.java
class
StringBufferExample6{
bli i id
Output:
16
16
34
7) StringBuffer ensureCapacity() method
The ensureCapacity() method of the StringBuffer class ensures that the given capacity is the
minimum to the current capacity. If it is greater than the current capacity, it increases the
capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
StringBufferExample7.java
class
StringBufferExample7{
bli i id
Output:
16
16
34
34
70
← Prev Next →
Youtube For Videos Join Our Youtube Channel: Join Now
Feedback
Send your Feedback to [email protected]
Help Others, Please Share
Learn Latest Tutorials
Keras tutorial Kibana Mean Stack
tutorial tutorial Microprocessor
Keras tutorial
Kibana Mean Stack
Microprocessor
Mobile Mobile Testing Nagios Nifi tutorial
Computing tutorial tutorial
Nifi
tutorial
Mobile Testing Nagios
Mobile Computing
online openstack PDF tutorial Photoshop
marketing tutorial tutorial
PDF
tutorial
OpenStack Photoshop
Online Marketing
Prolog PWA tutorial Pyspark
tutorial tutorial
PWA
Prolog Pyspark
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview
Questions
Company
Interview
Questions
Company
Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence
Hadoop ReactJS Data Science Angular 7
tutorial Tutorial Tutorial Tutorial
Hadoop ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial
Git
Tutorial
Blockchain DevOps
Machine Learning
B.Tech / MCA
DBMS Data DAA tutorial Operating
tutorial Structures System tutorial
DAA
tutorial
DBMS Operating System
Data Structures
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization Mathematics
and Tutorial
Computer Compiler Design
Architecture
Network Discrete
Computer Mathematics
Organization
Ethical Computer Software html tutorial
Hacking Tutorial Graphics Engineering
Web Technology
Tutorial Tutorial
Ethical Hacking
Computer Software
Graphics Engineering
Cyber Automata C Language C++ tutorial
Security tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python List of
Framework tutorial Programs
Java
tutorial
Python Programs
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse