Home  >  Core Java

Example of StringBuilder in Java

By Arvind Rai, February 21, 2013
StringBuilder has come in java since jdk 1.5. StringBuilder is not synchronized. It cannot be used in multi-threaded environment. StringBuffer can be used in multi-threaded environment. StringBuilder has the methods append, insert and appendCodePoint etc. appendCodePoint can add character ASCI value with the string as
sb.appendCodePoint(90);
 
90 is ASCII of Z.
package com.concretepage;
public class StringBuilderTest {
    public static void main(String... args) {
        StringBuilder sb = new StringBuilder("Hello");
        //appends the string 
        sb.append(" World");
        //insert the string at specified place
        sb.insert(6, " Great_ ");
        //90 ASCI of Z, it is appended at the end
        sb.appendCodePoint(90);
        System.out.println(sb);
        //gets capacity of string builder object
        int c = sb.capacity();
        System.out.println(c);
    }
}
Output
Hello  Great_ WorldZ
21
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us