Java StringBuffer Examples

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

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!