Capacity and length

ReturnMethodSummary
intcapacity()Returns the current capacity.
intlength()Returns the length (character count).
voidensureCapacity(int minimumCapacity)Ensures that the capacity is at least equal to the specified minimum.
voidsetLength(int newLength)Sets the length of the character sequence.
voidtrimToSize()Attempts to reduce storage used for the character sequence.

public class Main {
  public static void main(String[] argv) {
    StringBuilder sb = new StringBuilder();
    sb.append("java2s.com");

    System.out.println(sb.length());
    System.out.println(sb.capacity());

    
    sb.trimToSize();
    
    System.out.println(sb.length());
    System.out.println(sb.capacity());
    
  }
}

The output:


10
16
10
10
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.