An example on StringBuffer methods length() and capacity() is given in Simple terms for a Beginner.
Finding length() and capacity()
In Java coding, the programmer should be aware of not only methods of a class but also constructors. Constructors give the properties for an object at the time of creation itself. In the following program, three StringBuffer constructors are used.
Following are the signatures of length() and capacity() methods as given in StringBuffer class.
- int length(): Returns an integer value that represents the number of characters stored in the StringBuffer. Returns 0 if no characters exist.
- int capacity(): Returns an integer value that represents the buffer capacity of the StringBuffer.
public class LengthAndBuffer
{
public static void main(String args[ ] )
{
StringBuffer buffer1 = new StringBuffer( ) ;
StringBuffer buffer2 = new StringBuffer(50) ;
StringBuffer buffer3 = new StringBuffer("hello") ;
// to know the storing capacities
System.out.println("buffer1 capacity: " + buffer1.capacity()); // 16
System.out.println("buffer2 capacity: " + buffer2.capacity()); // 50
System.out.println("buffer3 capacity: " + buffer3.capacity()); // 21 (16 + 5 )
//to know the number of characters present
System.out.println("\nbuffer1 length: " + buffer1.length()); // 0
System.out.println("buffer2 length: " + buffer2.length()); // 0
System.out.println("buffer3 length: " + buffer3.length()); // 5
buffer3.ensureCapacity(150);
System.out.println("After modifying buffer3 capacity: " + buffer3.capacity()); // 150
}
}

StringBuffer buffer1 = new StringBuffer( ) ;
StringBuffer buffer2 = new StringBuffer(50) ;
StringBuffer buffer3 = new StringBuffer("hello") ;
Three StringBuffer objects, buffer1, buffer2 and buffer3, are created with different properties. buffer1 object gets by default a storage capacity of 16. Within this buffer of 16, string can be manipulated. If the string length crosses 16, another 16 characters buffer is added automatically. But no guarantee that another 16 buffer will be available besides the old buffer. Then system searches for 32 buffer in a single place, copies the old buffer contents and deletes the old buffer. It is again a overhead to the OS. That is, StringBuffer is not totally mutable, sometimes it becomes immutable when the capacity increases. To overcome this overhead, the programmer can request more size of the buffer initially itself. This type is used in buffer2 object creation. buffer2 gets an initial capacity of 50. The third object buffer3 gets implicitly 21 buffer as it already contains a string "hello" of length 5 characters.
buffer1.length() and buffer2.length() results in 0 as they do not contain any characters. Characters can be placed later. buffer3.length() results in 5, the number of characters present in the string "hello".
buffer3.ensureCapacity(150);
The capacity of a string buffer can be increased at any time in the program by calling ensureCapacity() method. Using this method, the existing capacity cannot be decreased.
For Comparisons
A novice should not get confused between String, StingBuffer and StringBuilder.
How to check whether given string is alphanumeric or not?
How to check whether given int is numberic?
Use Character class methods like isDigit() or isLetterOrDigit() etc.
http://way2java.com/java-lang/class-character/
Hello sir, i just want ask that is a good or bad way of coding?? see the code below… thanks
package stringHandling;
import java.util.Scanner;
public class Stringbuffer {
static String choice=””;
public static Scanner scanner = new Scanner(System.in);
static StringBuffer buffer1 = new StringBuffer();
static StringBuffer buffer2 = new StringBuffer(50);
static StringBuffer buffer3 = new StringBuffer(“Hello”);
public static void capacity(){
System.out.println(“Capacity of buffer1: “+buffer1.capacity());
System.out.println(“Capacity of buffer2: “+buffer2.capacity());
System.out.println(“Capacity of buffer3: “+buffer3.capacity());
buffer3.ensureCapacity(150);
System.out.println(“\nAfter modifying: “+buffer3.capacity());
}
public static void length(){
System.out.println(“\nLength of buffer1: “+buffer1.length());
System.out.println(“Length of buffer2: “+buffer2.length());
System.out.println(“Length of buffer3: “+buffer3.length());
}
public static void startOperation(){
System.out.println(“Enter A[capacity] B[length]”);
choice =scanner.nextLine();
if(choice.equalsIgnoreCase(“A”)){
capacity();
}
else if(choice.equalsIgnoreCase(“B”)){
length();
}
backToMain();
}
public static void backToMain(){
System.out.println(“Press M to go back to the main menu”);
choice=scanner.nextLine();
if(choice.equalsIgnoreCase(“M”)){
startOperation();
}
}
public static void main(String[] args) {
startOperation();
}
}
No problem in the code as it appears.
public class Twine {
public static void main(String[] args) {
String s = “”;
StringBuffer sb1 = new StringBuffer(“hi”);
StringBuffer sb2 = new StringBuffer(“hi”);
StringBuffer sb3 = new StringBuffer(sb2);
StringBuffer sb4 = sb3;
if(sb1.equals(sb2)) s += “1 “;
if(sb2.equals(sb3)) s += “2 “;
if(sb3.equals(sb4)) s += “3 “;
System.out.println(s);
}
}
o/p 3
string buffer does’t override equal, then howcome the output is 3
I remember, your code does not work on JDK 1.3. But I too found it is working in the present versions. Perhaps, the designer, for user friendliness, may be converting stringbuffer to string internally.
I like way2java and i read Way2java
thanks a lot.
sir,
why initial capacity of string buffer is 16?and capacity is 16+length of string? and not 16-length of string=?
Designers thought 16 is appropriate. 16+length is given to avoid immediate overhead of going to new location.
sir,
why the initila
StringBuffer b=new StringBuffer(“hello”);
System.out.println(b.capacity()); // its capacity will be16+5=21
b.append(“hello how are u mr”);
System.out.println(b.length()); //the total character now is 23
System.out.println(b.capacity()); //capacity 44 (how?)
through append() when characters crosses the 21 capacity limit then buffer should generate 16 buffer to the StringBuffer b and it should be 21+16=37 but why its showing the capacity to 44
No answer.
sir am i wrong somewhere
String Buffer capacity increase
new capacity=(old capacity*2)+2 or (old capacity+1)*2
Check with a program.