java中String類的建構函式

來源:互聯網
上載者:User

標籤:static   聲明   字元   []   buffer   類的建構函式   數組參數   拷貝   建構函式   

String類中的建構函式
String(); 構造一個Null 字元串對象
String(byte[] bytes); 通過byte數組構造字串對象
String(byte[] bytes,int offset,int length);通過byte數組,從offset開始,總共length長的位元組構造字串對象
String(char[] value); 通過char數組構造字串對象
String(byte[] char,int offset,int length);通過char數組,從offset開始,總共length長的位元組構造字串對象
String(String original); 構造一個original的副本,拷貝一個original
String(StringBuffer buffer);通過StringBuffer數組構造字串對象

public class StringClassTest {
    public static void main(String[] args) {
        // 位元組數組
        byte[] bArray = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘ };
        // 字元數組
        char[] cArray = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘ };
        //聲明一個StringBuffer
        StringBuffer strbuf = new StringBuffer("strbuf"); 
        // 執行個體一個String對象
        String str = new String("str abcd");
        //執行個體一個String對象 通過一個btye數組構造字串對象(位元組數組)
        String strb = new String(bArray);
        //執行個體一個String對象 通過一個char數組構造字串對象(字元數組)
        String strc = new String(cArray);
        //執行個體一個String對象 通過一個char數組構造字串對象(位元組數組,開始的資料,截得資料長度)
        String strbIndex = new String(bArray, 1, 5);
        //執行個體一個String對象 通過一個char數組構造字串對象(字元數組,開始的資料,截得資料長度)
        String strcIndex = new String(cArray, 1, 2);
        //執行個體一個String對象 通過一個StringBuffer物件建構字串對象 
        String strbuff = new String(strbuf);
        System.out.println("執行個體一個無參String對象:  "+str);
        System.out.println("執行個體一個帶byte數組參數String對象:  "+strb);
        System.out.println("執行個體一個帶char數組參數String對象:  "+strc);
        System.out.println("執行個體一個帶byte數組參數String對象,截取從1開始截取,截5位:  "+strbIndex);
        System.out.println("執行個體一個帶char數組參數String對象,截取從1開始截取,截2位:  "+strcIndex);
        System.out.println("執行個體一個帶StringBuffer參數String對象:  "+strbuff);
        // 如果是位元組類型,將輸出地址
        // System.out.println(by);
        // 如果是字元類型,將輸出字元
        // System.out.println(c);
    }
}

java中String類的建構函式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.