Java SE String類 [一]

來源:互聯網
上載者:User
 

 有兩個final類用於處理字元:String類和StringBuffer類。 1、String類實現不可變字元型字串,不可變字串在建立後和初始化之後能被讀取. 2、StringBuffer類實現動態字元型字串。 3、使用這兩個類實現的字元型字串是真正的對象,這種字串的每個字元都是用16位的字元表示的。  建立並初始化字串1、建立String對象的最簡單的辦法就是使用字串常量:     String str1="You cannot change me!"; /*可以從某種意義上說:A a=new A();A b=new A();a=b; *//*str1和”You cannot change me!”都是對象。*/  字串常量是String對象的引用。String對象中的值就是雙引號中字串常量的字元序列。由於字串常量是引用,因此可以像操作其他String引用一樣操作它。2、字串常量可以用來在其String對象上調用方法:      int len="You cannot change me!".length();3、interned字串<1>如果常量運算式的值位字串,而且常量運算式的字串序列都相同,那麼這些共用同一個String對象。字串為interned時,String類將護同一個私人池。      String str2="You cannot change me!";<2>字串引用str1和str2都代表同一個String對象,而且被初始化為字元型字串:"You cannot change me!"。<3>編譯時間在推導這個運算式時,將產生一個interned字串。<4>在下面的代碼裡,can1引用和can2參考資料表示同一個含有字串“7UP”的String對象。      String can1=7+"UP";      String can2="7UP";<5>但是,在下面的代碼裡,引用can4表示一個新的String對象,在編譯時間,其值為“7UP”。      String word="UP";      String can4=7+word;  值為字串的常量運算式共用同一個String對象不會產生問題,因為String對象是不可變的。運算式(7+其他字元)不是常量比運算式,因此產生了一個新的String對象。String建構函式        String類有許多建構函式,可以使用不同類型的參數來建立和初始化String對象。下面是其中的兩個建構函式:1、String (String s)此建構函式建立一個新的String對象,其內容與參數String對象的內容是一樣的。2、String( )<1>此建構函式建立一個新String對象,其內容為空白字串” ”。<2>請注意:使用建構函式將建立一個全新的String對象,而不是共用字串。<3>調用String類中intern( )方法可以獲得inerned字串的引用——但實際不需要這樣做。3、 其他建構函式。還可以使用位元組數組、字元數組和字串緩衝來建立String對象:     byte[ ]  bytes={97,98,98,97};     char[ ] characters={‘a’,’b’,’b’,’a’};     StringBuffer strBuf=new StringBuffer(“abba”);     //……     String byteStr=new String(bytes);            //Using array of bytes:”abba”     String charStr=new String(character);         //Using array of chars:”abba”     String buffStr=new String(strBuf);            //Using string of buffer:”abba”執行個體1、在下面的例子中,引用str1表示的String對象與str4和str5表示的都不一樣。2、使用new運算子和String建構函式總是建立新String對象。3、運算式("You cannot”+其他字元)不是常量運算式,因此產生了一個新Stiring對象。4、main( )方法中的本地引用str2和str3以及Auxiliary類中的靜態引用str1都表示同一個interned字串。代碼:package stringdemo; publicclass StringConstruction {     /**     *@paramargs     */    static String str1="You cannot change me!"; //Interned    publicstaticvoid main(String[] args) {       // TODO Auto-generated method stub       String emptyStr=new String();     //""       System.out.println("emptyStr"+emptyStr);              String str2="You cannot change me!";    //Interned       String str3="You cannot"+"change me!";    //Interned       String str4=new String("You cannot change me!"); // New String object              String words="change me";       String str5="You cannot"+words;         // New String object              System.out.println("str1==str2:"+(str1==str2));       //(1)true       System.out.println("str1.equals(str2)"+str1.equals(str2)); //(2)true              System.out.println("str1==str3:"+(str1==str3));           //(3)true       System.out.println("str1.equals(str3)"+str1.equals(str3));     //(4)true              System.out.println("str1==str4:"+(str1==str4));           //(5)false       System.out.println("str1.equals(str4)"+str1.equals(str4));     //(6)true              System.out.println("str1==str5:"+(str1==str5));           //(7)false       System.out.println("str1.equals(str5)"+str1.equals(str5));     //(8)true              System.out.println("str1==Auxiliary.str1:"+(str1==Auxiliary.str1));           //(9)true       System.out.println("str1.equals(Auxiliary.str1)"+str1.equals(Auxiliary.str1));     //(10)true              System.out.println("/"You cannot change me!/".length():"+"You cannot change me!".length());     } }class Auxiliary{    static String str1="You cannot change me!";} 

 

聯繫我們

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