Java學習筆記--String StringBuffer StringBuilder

來源:互聯網
上載者:User

標籤:

String StringBuffer StringBuilder

 

String http://docs.oracle.com/javase/7/docs/api/

中文: http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html

        String str0 = "最正常的建立方法";        System.out.println(str0);                char[] c1 = new char[3];        c1[0] = ‘c‘;        c1[1] = ‘b‘;        c1[2] = ‘a‘;        String str1 =new String("string1");        String str2 = new String(c1);        System.out.println(str1+ " "+ str2); //輸出string1 cba                /* String.valueOf()它可以將JAVA基本類型(int,double,boolean等)和對象(Object)轉換成String型         * toString()是對象的方法,它可以將該對象轉換成String型,轉換演算法根據類型實際需要而定,         * 基本上JAVA裡面每個對象都會有一個toString方法。*/        //字串轉換        int i1 = 100;        String str3= Integer.toString(i1);        System.out.println("Integer.toString : "+str3);//輸出Integer.toString : 100                //valueOf()進行資料轉換        String str32  = String.valueOf(123456);        System.out.println("String.valueOf() : " +str32);//輸出String.valueOf() : 123456                //字元抽取        //charAt        String str4 = "A long String!";        System.out.println("charAt(3) : "+str4.charAt(3));//輸出charAt(3) : o        //getChars()        char buf [] = new char[6-2];        str4.getChars(2, 6, buf    , 0);        System.out.print("getChars輸出 :");        System.out.println(buf); //輸出getChars輸出 :long        //char[] toCharArray()        System.out.println(str4.toCharArray());//輸出A long String!        //抽取子串        String str5 = str4.substring(2,8);        System.out.println("subString(2,8)輸出 : "+str5);//輸出subString(2,8)輸出 : long S    

 

StringBuffer http://docs.oracle.com/javase/7/docs/api/

        /*         * StringBuffer()         * StringBuffer(int size)         * StringBuffer(String str)          * 接收一個String變元,指定StringBuffer對象的初始內容,並額外預留16個字元空間         * StringBuffer(CharSequence chars)         */        StringBuffer sbf = new StringBuffer("Hello");        System.out.println("buffer = " +sbf);        System.out.println("length = " +sbf.length());        System.out.println("capacity = " +sbf.capacity());                System.out.println("----重新指定緩衝區大小----");        sbf.ensureCapacity(55);        System.out.println("buffer = " +sbf);        System.out.println("length = " +sbf.length());        System.out.println("capacity = " +sbf.capacity());                System.out.println("----設定緩衝區長度----");        sbf.setLength(30);        System.out.println("buffer = " +sbf);        System.out.println("length = " +sbf.length());        System.out.println("capacity = " +sbf.capacity());        

輸出結果:

buffer = Hellolength = 5capacity = 21----重新指定緩衝區大小----buffer = Hellolength = 5capacity = 55----設定緩衝區長度----buffer = Hello

 

StringBuilder http://docs.oracle.com/javase/7/docs/api/

除了一個重要的區別之外,StringBuffer等同於StringBuilder,區別是:它不是同步的,即意味著它不是安全執行緒的。

StringBuilder優勢在於具有更快的效能,但是使用多線程時必須使用StringBuffer,而不能使用StringBuilder

Java學習筆記--String StringBuffer StringBuilder

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.