0% found this document useful (0 votes)
10 views

Java Lec-16 StringBuffer Class.14a2a2e

The document discusses the StringBuffer class in Java, which represents mutable character strings. It describes several constructors and methods of the StringBuffer class like append(), insert(), delete(), and reverse(). It also compares the differences between the immutable String class and mutable StringBuffer class.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Lec-16 StringBuffer Class.14a2a2e

The document discusses the StringBuffer class in Java, which represents mutable character strings. It describes several constructors and methods of the StringBuffer class like append(), insert(), delete(), and reverse(). It also compares the differences between the immutable String class and mutable StringBuffer class.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Course: Java Programming,Prepared By: Atul Kabra, 9422279260

Java Lecture- 16
Topic: StringBuffer Class

 Java provides the String and StringBuffer classes to handle String.

 The String class is used to manipulate character strings that cannot be


changed. The string which cannot be changed is called as immutable string.

 The StringBuffer class is used to represent character String that can be


changed after it is created. The string which can be changed is called as
mutable string.

 StringBuffer represents growable and writable character sequence.

 The significant performance difference between these two classes is


that StringBuffer is faster than String when performing simple
concatenations.

Constructors of StringBuffer class


StringBuffer defines 4 constructors.

1. StringBuffer(): It creates an empty string buffer and reserves space for 16


characters.

2. StringBuffer(int size): It creates an empty string and takes an integer


argument to set capacity of the buffer.

3. StringBuffer(String str): It creates a stringbuffer object from the specified


string.

4. StringBuffer(char []ch): It creates a stringbuffer object from the


charsequence array.

Course: Java Programming Prepared By: Atul Kabra, 9422279260


Course: Java Programming,Prepared By: Atul Kabra, 9422279260

Methods of StringBuffer class:


1. append()
This method will concatenate the string representation of any type of data
to the end of the StringBuffer object. append() method has several
overloaded forms.
Syntax: StringBuffer append(T value)
Here T can be any type like String, int , float, char etc.
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.append(“Java”);
System.out.println(sb);
Ouput: HelloWorldJava

2. insert()
This method inserts one string into another.
Syntax: StringBuffer insert(int index, T value)
Here the first parameter gives the index at which position the
string will be inserted and T representation of second
parameter is inserted into StringBuffer object.
Here T can be any type like String, int , float, char etc.
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.insert(5, “Java”);
System.out.println(sb);
Ouput: HelloJavaWorld

Course: Java Programming Prepared By: Atul Kabra, 9422279260


Course: Java Programming,Prepared By: Atul Kabra, 9422279260

3. delete ()
This method removes the characters from the string.
Syntax: StringBuffer delete (int start, int end)
This method is used to delete the string from specified
startIndex and endIndex. Here endindex is exclusive.
Example: StringBuffer sb = new StringBuffer(“HelloJavaWorld”);
sb.delete(5,9);
System.out.println(sb);
Ouput: HelloWorld

4. deleteCharAt()
This method removes the character at the specified index character from
the string.
Syntax: StringBuffer deleteCharAt (int index)
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.deleteCharAt(4);
System.out.println(sb);
Ouput: HellWorld

5. replace ()
This method replaces the specified string from specified start index to the
end index.
Syntax: StringBuffer replace (int start, int end, String newString)
Example: StringBuffer sb = new StringBuffer(“HelloJavaWorld”);
sb.replace(5,9,”InfoPlanet”);
System.out.println(sb);
Ouput: HellInfoPlanetWorld

Course: Java Programming Prepared By: Atul Kabra, 9422279260


Course: Java Programming,Prepared By: Atul Kabra, 9422279260

6. reverse()
This method reverses the characters within a StringBuffer object.
Syntax: StringBuffer reverse ()
Example: StringBuffer sb = new StringBuffer(“HelloWorld”);
sb.reverse();
System.out.println(sb);
Ouput: dlroWolleH

Note: Refer Java API documentation for more methods.

Difference between String and StringBuffer.

Sr. String StringBuffer


No.
1 Characters of String cannot be Characters of StringBuffer can be
changed. changed.
2 String is immutable. StringBuffer is mutable.
3 Processing of String is slow. Processing of StringBuffer is fast.
4 At the time of String concatenation, At the time of String
String consumes more memory concatenation, StringBuffer
space. consumes less memory space.

Course: Java Programming Prepared By: Atul Kabra, 9422279260

You might also like