Java StringBuffer length and capacity

Introduction

The length() method returns the current length of a StringBuffer.

The capacity() method returns the allocated capacity.

They have the following general forms:

int length()  
int capacity() 

Here is an example:

// StringBuffer length vs. capacity.
public class Main {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("Hello");

    System.out.println("buffer = " + sb);
    System.out.println("length = " + sb.length());
    System.out.println("capacity = " + sb.capacity());
  }//from  w ww.jav  a  2  s  . co  m
}



PreviousNext

Related