String Buffer
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]
• StringBuffer( ) : The default constructor (the one with no parameters)
reserves room for 16 characters without reallocation.
• StringBuffer(int size) : accepts an integer argument that explicitly sets
the size of the buffer.
• StringBuffer(String str) : sets the initial contents of the StringBuffer
object and reserves room for 16 more characters without
reallocation.
• StringBuffer(CharSequence chars) : contains the character sequence
contained in chars and reserves room for 16 more characters.
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 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]
S.No String String Buffer
1 String class is immutable StringBuffer class is mutable.
2 String is slow and consumes more memory when StringBuffer is fast and consumes less memory
you concat too many strings because every time when you cancat strings
it creates new instance.
3 String class overrides the equals() method of StringBuffer class doesn't override the equals()
Object class. So you can compare the contents of method of Object class.
two strings by equals() method.
String Builder Class: [2]
• Introduced by JDK 5, StringBuilder is a relatively recent addition to
Java’s string handling capabilities.
• StringBuilder is similar to StringBuffer except for one important
difference: it is not synchronized, which means that it is not thread-
safe.
• The advantage of StringBuilder is faster performance.
• However, in cases in which a mutable string will be accessed by
multiple threads, and no external synchronization is employed, you
must use StringBuffer rather than StringBuilder.
String Builder Constructors: [3]
String Builder Methods:
Append() method:
class StringBuilderExample
{
public static void main(String args[])
{
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Note: More examples in reference link [3]
String Buffer VS String Builder: [4]
S.No String Buffer String Builder
1 StringBuffer is synchronized i.e. thread safe. It StringBuilder is non-synchronized i.e. not thread
means two threads can't call the methods of safe. It means two threads can call the methods of
StringBuffer simultaneously. StringBuilder simultaneously.
2 StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.
Reference:
[1] https://www.javatpoint.com/difference-between-string-and-stringbuffer
[2] Java: The Complete Reference
[3] https://www.javatpoint.com/StringBuilder-class
[4] https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder
Thanks