OOPs - Lecture 12 - String Buffer, String Builder
OOPs - Lecture 12 - String Buffer, String Builder
and
String Builder
Lecture 12
Learning Outcome
After the completion of today’s session you will be able to
• Learn about String Buffer and String Builder class.
• Analyze the constructors used in both class.
• Illustrate String Buffer and String Builder class with an example.
String Buffer Class:
• Java StringBuffer class is used to create mutable (modifiable) string.
• The StringBuffer class in java is same as String class except it is
mutable i.e. it can be changed.
• StringBuffer object is also stored in the heap not shared, which helps
us to modify them without any side effects on other objects.
String Buffer Class – contd…
• String represents fixed-length, immutable character sequences. In
contrast, StringBuffer represents growable and writable character
sequences.
• StringBuffer may have characters and substrings inserted in the
middle or appended to the end.
• StringBuffer will automatically grow to make room for such additions
and often has more characters preallocated than are actually needed,
to allow room for growth.
String Buffer constructors: [2]
Note : Study other String Buffer methods from Java : The complete reference
String Buffer Methods: length() and
capacity()
// StringBuffer length vs. capacity.
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer = " + sb);
System.out.println("length = " + sb.length());
System.out.println("capacity = " + sb.capacity());
}
}
Note : Study other String Buffer methods from Java : The complete reference
String VS String Buffer: [1]