Skip to Content

Java StringBuffer Examples

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

StringBuffer is a mutable sequence of characters. It is like a String but the contents of the StringBuffer can be modified after creation.

StringBuffer Capacity
Every StringBuffer object has a capacity associated with it. The capacity of the StringBuffer is the number of characters it can hold. It increases automatically as more contents added to it.

StringBuffer’s current capacity can be obtained using following method.

  1. int capacity()

This method returns the current capacity of the StringBuffer object.

For example,

  1. StringBuffer stringBuffer = new StringBuffer(“Hello World”);
  2. System.out.println(stringBuffer.capacity());

This will print 27 (11 + 16) on console when executed.

The actual number of characters in StringBuffer can be obtained by following method.

  1. int length()

Returns the actual number of characters contained in the StringBuffer object.

For example,

  1. StringBuffer stringBuffer = new StringBuffer(“Hello World”);
  2. System.out.println(stringBuffer.length());
  3. System.out.println(stringBuffer.capacity());

This will print,
11
27
on console when run.

Specifying initial capacity of StringBuffer
Initial capacity of the StringBuffer object can be specified using following method.

  1. void ensureCapacity(int initialCapacity)

Ensures that the StringBuffer’s initial capacity would be grater than or equal to the specified initial capacity.

The new capacity of the StringBuffer is the maximum of,
1) The initialCapacity argument passed and
2) ( Old capacity * 2 ) + 2

StringBuffer Constructors
StringBuffer object can be created using following StringBuffer constructors.

1) StringBuffer()
Creates empty StringBuffer object having initial capacity of 16.

For example,

  1. StringBuffer stringBuffer = new StringBuffer();

Here, capacity of stringBuffer object would be 16.

2) StringBuffer(int capacity)
Creates empty StringBuffer object having initial capacity specified.

For example,

  1. StringBuffer stringBuffer = new StringBuffer(50);

Here, capacity of stringBuffer object would be 50.
This constructor may throw NegativeArraySizeException if the passed argument is less than 0.

3) StringBuffer(String content)
Creates new StringBuffer object having contents same as the argument string object. The initial capacity of the newly created StringBuffer object will be the length of the argument string object + 16.

For example,

  1. String str = “Hello”;
  2. StringBuffer stringBuffer = new StringBuffer(str);

Here, capacity of stringBuffer object would be 5 + 16 = 21.

String concatenation and StringBuffer
In Java, String concatenation operator (+) is internally implemented using StringBuffer.

For Example,

  1. String str = “Hello” + “World”;

Would be executed as,
  1. String str = new StringBuffer().append(“Hello”).append(“World”).toString();

First an empty StringBuffer will be created and then operands of the + operator would be appended using append method of the StringBuffer. Finally toString() method of the StringBuffer would be called to convert it to string object and the same will be returned.

String concatenation using this approach is very expensive in nature, because it involves creation of temporary StringBuffer object. Then that temporary object’s append method gets called and the resulting StringBuffer would be converted back to String using toString() method.

When to use String and when StringBuffer?
If there is a need to change the contents frequently, StringBuffer should be used instead of String because StringBuffer concatenation is significantly faster than String concatenation.

Java StringBuffer Examples