Tuesday 26 April 2016

Java Tutorial : Java StringBuilder Introduction


Click here to watch in Youtube :
https://www.youtube.com/watch?v=KZySy62D1gk&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java StringBuilder Introduction 
Java Tutorial : Java StringBuilder Introduction 
Java Tutorial : Java StringBuilder Introduction 

StringBuilderDemo.java
public class StringBuilderDemo
{

    public static void main(String[] args)
    {
        /*
         * Constructs a string Builder with no characters in
         * it and an initial capacity of 16 characters.
         */
        StringBuilder sb1 = new StringBuilder();
        System.out.println("sb1 = " + sb1);
        System.out.println("sb1 capacity = " + sb1.capacity());

        System.out.println("-----------------------");

        /*
         * Constructs a string Builder with no characters in
         * it and the specified initial capacity.
         */
        StringBuilder sb2 = new StringBuilder(50);
        System.out.println("sb2 = " + sb2);
        System.out.println("sb2 capacity = " + sb2.capacity());

        System.out.println("-----------------------");

        /*
         * Constructs a string Builder initialized to the
         * contents of the specified string. The initial
         * capacity of the string Builder is 16 plus the
         * length of the string argument.
         */
        String str = "Hello";
        StringBuilder sb3 = new StringBuilder(str);
        System.out.println("sb3 = " + sb3);
        System.out.println("sb3 capacity = " + sb3.capacity());

        System.out.println("-----------------------");

        /*
         * Constructs a string Builder that contains the same
         * characters as the specified CharSequence. The
         * initial capacity of the string Builder is 16 plus
         * the length of the CharSequence argument.
         */
        CharSequence charSequence = new StringBuilder("Welcome");
        StringBuilder sb4 = new StringBuilder(charSequence);
        System.out.println("sb4 = " + sb4);
        System.out.println("sb4 capacity = " + sb4.capacity());

    }
}
Output
sb1 = 
sb1 capacity = 16
-----------------------
sb2 = 
sb2 capacity = 50
-----------------------
sb3 = Hello
sb3 capacity = 21
-----------------------
sb4 = Welcome
sb4 capacity = 23
Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/StringBuilder.html
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringBuilderDemo_Intro_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ea6705eb8d861d880326570ef3992b3db7646c7d/BasicJava/StringBuilderDemo_Intro_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