Friday, December 25, 2015

length() and capacity() method

StringBuffer:



length() and capacity()

The length of the StringBuffer can be found by the length() method.while the total capacity can be found through the capacity() method.They have the following forms:

                      int length();
                      int capacity();




package stringdemo;


public class LengthAndCapacity {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("Dance");
        
        System.out.println("Length="+s.length());
        System.out.println("Capacity"+s.capacity());
    }
}
OUTPUT:
Length=5
Capacity21

Here Length is 5 and capacity is 21 because room for 16 additional characters is automatically added,so result is 21.

Related Posts:

  • Increment And Decrement Operator Increment and Decrement Operator: package javaapplication16; public class IncrementAndDecrementOPeratorDemo {     public static void mai… Read More
  • Arithmetic Operators Arithmetic Operators: package javaapplication16; public class ArithemiticOperatorDemo {     public static void main(String[] args) { &… Read More
  • Relational Operator Relational Operator: Operators                             Result ==… Read More
  • Java KingKong Program Java KingKong Program: package javaapplication16; public class KingKongDemo {     public static void main(String[] args) { &nb… Read More
  • Stack in java: Stack in java: package javaapplication27; import java.util.Stack; public class TestStack {         public static void main(Str… Read More

0 comments:

Post a Comment