Monday 2 May 2016

Java Tutorial : Java StringBuilder [trimToSize() method]

StringBuilderDemo.java
/*
 * public void trimToSize()
 * 
 * Attempts to reduce storage used for the character
 * sequence. If the buffer is larger than necessary to
 * hold its current sequence of characters, then it may
 * be resized to become more space efficient. Calling
 * this method may, but is not required to, affect the
 * value returned by a subsequent call to the capacity()
 * method.
 */

public class StringBuilderDemo
{

    public static void main(String[] args)
    {

        StringBuilder sb = new StringBuilder("Welcome");
        
        System.out.println("Before timeToSize,Capacity =  "
                + sb.capacity());

        sb.trimToSize();
        
        System.out.println("After timeToSize,Capacity  =  "
                + sb.capacity());
    }
}
Output
Before timeToSize,Capacity =  23
After timeToSize,Capacity  =  7

Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringBuilderDemo_trimToSize_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringBuilderDemo_trimToSize_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/bb94d904a4b291ce0b8aeae9cc6d268e0b410985/BasicJava/StringBuilderDemo_trimToSize_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment