Java StringBuffer Sample Code

Let’s talk about StringBuffer. The StringBuffer Class is a sequence of characters that is thread safe and mutable. If your unclear on mutable vs immutable, a Java String is immutable meaning it can’t be modified.

On the other hand, the Java StringBuffer is mutable meaning it can be modified, which is nice. Because the methods of the StringBuffer class are synchronized when necessary, it is safe for use by multiple threads.


Create StringBuffer Object

Here we create a new StringBuffer Object that is empty and another where we include a string of characters.

//Create an empty StringBuffer Object
StringBuffer jcdStringBufferEmpty = new StringBuffer();

//Create a StringBuffer Object
StringBuffer jcdStringBuffer = new StringBuffer("JavaCodeDepot Is Da Bomb!");


StringBuffer Capacity Method

The StringBuffer capacity() method will return the current capacity, which is the amount of storage available for newly inserted characters. In our example, the capacity is 41 characters even though the length of our string is 25. Why? Because StringBuffer will add 16 characters of space.

//Get the capacity
System.out.println("Capacity: "+jcdStringBuffer.capacity());
Capacity: 41


StringBuffer Length Method

The StringBuffer length() method does just what is says, returns the length of string or the character count.

//Get the length
System.out.println("Length: "+jcdStringBuffer.length());
Length: 25


StringBuffer Append Method

StringBuffer has 13 different append() methods to use. Here we are using the append(String) version to add some characters to our empty StringBuffer.

//Append some characters to our empty StringBuffer object
jcdStringBufferEmpty.append("JavaCodeDepot.com");
		
//Get the capacity again
System.out.println("Capacity: "+jcdStringBufferEmpty.capacity());
						
//Get the length again
System.out.println("Length: "+jcdStringBufferEmpty.length());
Capacity: 34
Length: 17


Java StringBuffer Sample Source Code

Here is the full working Java source code that was written for this tutorial.

package com.jcd.java.tutorials;

public class JavaStringBuffer 
{

	public static void main(String[] args) 
	{
	
		//Create an empty StringBuffer Object
		StringBuffer jcdStringBufferEmpty = new StringBuffer();
		
		//Print it out
		System.out.println("\njcdStringBufferEmpty: "+jcdStringBufferEmpty.toString());
		
		//Get the capacity
		System.out.println("Empty Capacity: "+jcdStringBufferEmpty.capacity());
				
		//Get the length
		System.out.println("Empty Length: "+jcdStringBufferEmpty.length());
				
		//Append some characters to our empty StringBuffer object
		jcdStringBufferEmpty.append("JavaCodeDepot.com");

		//Print it out
		System.out.println("\njcdStringBufferEmpty: "+jcdStringBufferEmpty.toString());
		
		//Get the capacity again
		System.out.println("Capacity: "+jcdStringBufferEmpty.capacity());
						
		//Get the length again
		System.out.println("Length: "+jcdStringBufferEmpty.length());
		
		
		//Create a StringBuffer Object
		StringBuffer jcdStringBuffer = new StringBuffer("JavaCodeDepot Is Da Bomb!");
		
		//Print it out
		System.out.println("\njcdStringBuffer: "+jcdStringBuffer.toString());
		
		//Get the capacity
		System.out.println("Capacity: "+jcdStringBuffer.capacity());
		
		//Get the length
		System.out.println("Length: "+jcdStringBuffer.length());
		
	}

}
jcdStringBufferEmpty: 
Empty Capacity: 16
Empty Length: 0

jcdStringBufferEmpty: JavaCodeDepot.com
Capacity: 34
Length: 17

jcdStringBuffer: JavaCodeDepot Is Da Bomb!
Capacity: 41
Length: 25



Concluding Comments

If you just need to define some characters and don’t need to change them later, use the String class. However is there is a need to define and later modify a string of characters, StringBuffer is your best option.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *