StringBuilder

* String:不可变字符序列
* StringBuild:不可变字符序列,效率高
* StringBuffer:线程安全

package mypro01;

/**
 * String:不可变字符序列
 * StringBuild:不可变字符序列,效率高
 * StringBuffer:线程安全
 * @author 4090039qrh
 *
 */

public class Test08 {
    
    public static void main(String[] args) {
        StringBuilder sb=new StringBuilder();   //字符数组初始长度为16
        StringBuilder sb1=new StringBuilder(32);  //字符数组初始长度为32
        StringBuilder sb2=new StringBuilder("abc");//value[]={'a','b','c'}
        
        //append 参数当作字符串处理
        sb2.append("efg");
        sb2.append(true);
        sb2.append(456);
        sb2.append("efg").append(true).append(456);//通过return this 实现方法链
        
        Animal a=new Animal(18);
        sb2.append(a);
        System.out.println(sb2);
        
        StringBuilder gh=new StringBuilder("a");        
        for(int i=0;i<100;i++) {
            gh.append(i);
            
        }
                
        System.out.println(gh);
        
        StringBuilder sb3=new StringBuilder("abcdefghigk");    
        //delete
        sb3.delete(3, 5);
        
        //reverse
        sb3.reverse();
        System.out.println(sb3);
        
    }

}
package mypro01;

public class Animal {
    
    int age;
    
    public Animal(int age) {
        this.age = age;
        
    }   
    
}

 

posted on 2020-03-06 14:47  happygril3  阅读(100)  评论(0)    收藏  举报

导航