Вы находитесь на странице: 1из 14

String Buffer

String
String is one of the most important classes in Java.
String is immutable and final in Javaand every
modification in String result creates a new String object.

Immutability offers lot of benefit to theString classe.g. its


hashcode value can be cached which makes it a faster
hashmap key and one of the reasonwhy String is a popular
key in HashMap. Because String is final it can be safely
shared
between
multiple
threads
without
any
extrasynchronization.

Disadvantage of String
Many times we create a String and then perform a lot of
operation on them e.g. converting string into uppercase,
lowercase, gettingsubstringout of it , concatenating with
other string etc.
Since String is an immutable class every time a new String is
created and older one is discarded which creates lots of
temporary garbage in heap.
If String are created using String literal they remain in String
pool.
To resolve this problem Java provides us two
ClassesStringBufferand StringBuilder.
String Buffer is an older class but StringBuilder is relatively
new and added in JDK 5.

StringBuffer
StringBufferis mutable means you can modify
aStringBufferobject once you created it without
creating any new object.

StringBuffer
The length and content of the StringBuffer
sequence can be changed through certain
method calls.
StringBuffer defines three constructors:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)

CSM-Java Programming-I

Lesson-1

StringBuffer Operations
The principal operations on a StringBuffer are the append and insert
methods, which are overloaded so as to accept data of any type.
Here are few append methods:
StringBuffer append(String str)
StringBuffer append(int num)

The append method always adds these characters at the end of the
buffer.

booleanb=true;
StringBuffersb1=newStringBuffer("BooelanAppended : ");
sb1.append(b);
System.out.println(sb1)

BooelanAppended : true

CSM-Java Programming-I

Lesson-1

StringBuffer Operations
The insert method adds the characters at a specified
point.
Here are few insert methods:
StringBuffer insert(int index, String str)
StringBuffer append(int index, char ch)

Index specifies at which point the string will be inserted


into the invoking StringBuffer object.
booleanb=true;
StringBuffersb4=newStringBuffer("Hello
World");
sb4.insert(6,b);
System.out.println(sb4);
Hello true World

CSM-Java Programming-I

Lesson-1

StringBuffer Operations
delete() - Removes the characters in a substring
of this StringBuffer. The substring begins at the
specified start and extends to the character at
index end - 1 or to the end of the StringBuffer if
no such character exists. If start is equal to end,
no changes are made.
public StringBuffer delete(int start, int end)
StringBuffersb1=newStringBuffer("Hello
World");
sb1.delete(0,6);
System.out.println(sb1);
World
CSM-Java Programming-I

Lesson-1

StringBuffer deleteCharAt(int index) deletes the


character at specified index.
StringBuffersb3=newStringBuffer("Hello
World");
sb3.deleteCharAt(0);
System.out.println(sb3);
O/P - ello World

replace() - Replaces the characters in a substring


of this StringBuffer with characters in the
specified String.
public StringBuffer replace(int start, int end,
String str).


StringBuffersb=newStringBuffer("Hello
World");

System.out.println("Original Text :
"+sb);
sb.replace(0,5,"Hi");

System.out.println("Replaced Text :
"+sb);
Output would be
CSM-Java
Programming-I
Lesson-1
Original
Text : Hello
World

substring() - Returns a new String that contains a


subsequence of characters currently contained in this
StringBuffer. The substring begins at the specified
index and extends to the end of the StringBuffer.
public String substring(int start)
StringBuffersb=newStringBuffer("Java
StringBuffer SubString Example");
System.out.println("Original Text : "+sb);
StringstrPart1=sb.substring(5);
System.out.println("Substring 1 : "+strPart1);
StringstrPart2=sb.substring(0,17);
System.out.println("Substring 2 : "+strPart2);

Output would be
Original Text : Java StringBuffer SubString
Example
Substring 1 : StringBuffer SubString Example
Substring 2 : Java StringBuffer

StringBuffer Operations
reverse() - The character sequence contained in
this string buffer is replaced by the reverse of the
sequence.
public StringBuffer reverse()

StringBuffersb=newStringBuffer("Java
StringBuffer Reverse Example");
System.out.println("Original StringBuffer
Content : "+sb);
sb.reverse();
System.out.println("Reversed
StringBuffer Content : "+sb);
Original StringBuffer Content :
CSM-Java
Lesson-1
Java Programming-I
StringBuffer Reverse
Example

setLength() - Sets the length of the StringBuffer.


public void setLength(int newLength

StringBuffersbf=newStringBuffer("StringBu
ffer setLength method example");
/* If newLegth is less than the original length, contents of
StringBuffer would be truncated. If newLength is grater than the
original length, StringBuffer would be filled with null characters
('\u0000'). */

sbf.setLength(12);
System.out.println("StringBuffer contents:
"+sbf);
sbf.setLength(0);
System.out.println("StringBuffer contents
deleted:"+sbf);
Output of Java StringBuffer setLength
example would be

Further Reading
http://javarevisited.blogspot.com/2
011/07/string-vs-stringbuffer-vs-st
ringbuilder.html
http://www.java-examples.com/java
-stringbuffer-examples

Вам также может понравиться