Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java String-examples Creation of StringBuffer

Related Tutorials


 
 

Share on Google+Share on Google+

Creation of StringBuffer

Advertisement
In this section, you will learn how to create a string buffer.

Creation of StringBuffer

     

In this section, you will learn how to create a string buffer. Here we have used the following StringBuffer Constructors.

StringBuffer() //Default Constructor with 16 characters set
StringBuffer(String s) //String to String Buffer
StringBuffer(int initialCapacity) //StringBuffer?s with initial Capacity 

Description of code:

As mentioned above we have taken three String buffer constructors here. One is the default with 16 characters and other two takes the String buffer and capacity of StringBuffer. Therefore we get the following output.

public class StringBufferExample{
  public static void main(String[] args) {
  StringBuffer strBuffer1 = new StringBuffer("Bonjour");  
  StringBuffer strBuffer2 = new StringBuffer(60);  
  StringBuffer strBuffer3 = new StringBuffer();  
  
  System.out.println("strBuffer1 : " +strBuffer1);
  System.out.println("strBuffer2 capacity : " +strBuffer2.capacity());
  System.out.println("strBuffer3 capacity : " +strBuffer3.capacity());
  }
}

Output of the program:

C:\unique>javac StringBufferExample.java

C:\unique>java StringBufferExample
strBuffer1 : Bonjour
strBuffer2 capacity : 60
strBuffer3 capacity : 16

C:\unique>

Download this example.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2007

Related Tutorials

Discuss: Creation of StringBuffer   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Bhushan
May 10, 2012
about String and related classes of java

my question is, How exactly String class in java is different from StringBuffer and StringBuilder classes. it will be more helpful if explained with examples.
DMCA.com