Java String類

來源:互聯網
上載者:User

標籤:[]   構造方法   判斷字串   緩衝區   nbsp   不可變   開始   gtest   ted   

java.lang.String;是字串類型,關於String類,首先需要注意以下幾點:

1.字串一旦建立不可改變。“abc”字串對象一旦建立,不可再變成“abcd”;

2.提升字串的訪問效率:在程式中使用了“緩衝”技術。所以在Java中所有使用“雙引號”

括起來的字串都會在“字串常量池”中建立一份。字串常量池在方法區中被儲存。

3.在程式執行過程中,如果程式用到某個字串,例如"abc",那麼程式會在字串常量池

中建立一個“abc”字串,如果找到就直接拿來用。(字串常量池是一個緩衝區,為了提高訪問字串的效率)

 

public class StringTest01{
 public static void main(String[] args){
   //建立一個“abc”字串對象,該對象的記憶體位址,讓s1變數儲存。
   //s1是一個引用,s1指向“abc”對象。
   String s1="abc";
   //可以讓s1重新指向
   //但是"def"字串本身不可變
   s1="def";
   String s2="hello";  //在字串常量池中建立一個"hello"字串對象,該對象不可變
   String s3="hello";  //從字串常量池中直接拿來用
   System.out.println(s2==s3);  // true
   //判斷字串是否相等不能用==
   String s4=new String("abc");
   String s5=new String("abc");
   System.out.println(s4==s5);  //false
   //以下程式在執行結束之後,會在字串常量池中建立三個字串對象"aaa""bbb""aaabbb"
   String s6="aaa";
   String s7="bbb";
   String s8=s6+s7;
 }
}

 

上述代碼中,首先建立一個“abc”字串對象,並且該對象的記憶體位址,讓s1變數儲存,也就是說s1是一個引用,指向“abc”對象。s1="def";是將s1重新指向字串"def",但是"def"字串本身不可變。接著建立了一個新的字串s2,String s2="hello";在字串常量池中建立一個"hello"字串對象,該對象不可變,接下來再建立"hello"字串時發現字串常量池中已經存在這個字串了,就不需要再重新建立,直接拿過來就可以用,也就是剛開始講的第三條,這樣做可以提高訪問字串的效率。使用String的時候我們還應該注意:盡量不要做字串頻繁的拼接操作,如上面的String s8=s6+s7,因為字串一旦建立不可改變,只要頻繁拼接,就會在字串常量池中建立大量的字串對象,給記憶體回收帶來問題。

 

接下來看看不同的方法建立字串對象的區別。

1.String s1="abc";只會在字串常量池中建立一個“abc”字串對象;

2.String s2=new String("hello");會在字串常量池中建立一個“hello”字串對象,並且在堆中再建立一個字串對象。

第二種方式比較浪費記憶體,常用的是第一種方式。好了,接下來做一道題吧,運用剛剛講的知識判斷以下程式建立了了幾個對象?

 

public class StringTest03{
 public static void main(String[] args){
   String s1=new String("hello");
   String s2=new String("hello");
   
 }
}

 

答案是三個,不知道大家有沒有做對?

String s1=new String("hello");

String s2=new String("hello");

new出兩個字串對象,所以在堆中有兩個對象,因為在Java中所有使用“雙引號”括起來的字串都會在“字串常量池”中建立一份,字串常量池在方法區中被儲存,所以方法區的字串常量池中一個,堆中的兩個加常量池中的一個,就是最後的答案。

 

最後再來看看關於字串常用的構造方法。

 

/*
關於字串常用構造方法
*/
public class StringTest05{
 public static void main(String[] args){
   //1.
   String s1="abc";
   //2.
   String s2=new String("abc");
   //3.
   byte[] bytes={97,98,99,100};
   String s3=new String(bytes);
   System.out.println(s3);   //abcd   String已經重寫了Object中的toString方法
   //4.
   String s4=new String(bytes,1,2);   //1代表起始下標,2代表長度
   System.out.println(s4);   //bc
   //5.
   char[] c1={‘我‘,‘是‘,‘中‘,‘國‘,‘人‘};
   String s5=new String(c1);
   System.out.println(s5);   //我是中國人
   //6.
   String s6=new String(c1,2,2);
   System.out.println(s6);  //中國
   }
}

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.