String Buffer
String Buffer
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.
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)
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
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
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
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