java_String、集合類

來源:互聯網
上載者:User

標籤:效率   cep   rup   inter   過程   thread   運行   記憶體   最佳化   

1.==與equals

1)a==b是進行地址比較,a.equals(b)是進行值比較。

2)String類是final不可變類,對String的改變都會引發新的String對象的產生。

3)“abc”形式會分配到字串常量池。

4)JVM對於字串常量的"+"號串連,編譯期就將常量字串的"+"串連最佳化為串連後的值。

5)如果在字串+操作過程中,有引用存在,如t="a"; b = t+"bc";中的t,則運行期才能獲得t記憶體塊,顯然不會最佳化為"abc"常量。但在變數前加個final,也會在編譯期儲存為常量池中或嵌入到它的位元組碼流中。

2.用String拼接與StringBuilder拼接

3.String、StringBuilder、StringBuffer

當運算量不是很高時,可以不用考慮效能問題;

單線程時,當運算量百萬層級時,選StringBuilder;

多線程時,當運算量百萬層級時,選StringBuffer;

package day_3.String;public class TestString {static void test_1(){int i=0;while (true) {String a = "abc";//String a = new String("abc");try {Thread.sleep(10);System.out.println(i++);} catch (InterruptedException e) {e.printStackTrace();}a = null;}}/** *  1)a==b是進行地址比較,a.equals(b)是進行值比較。2)String類是final不可變類,對String的改變都會引發新的String對象的產生。3)“abc”形式會分配到字串常量池。 */static void test_2(){//String a = "abc"; //String b = "abc";//System.out.println("a==b : " + (a==b)); //true//System.out.println("a.equals(b)  : " + a.equals(b) ); //true//String a = new String("abc"); //String b = new String("abc"); //System.out.println("a==b : " + (a==b)); //false//System.out.println("a.equals(b)  : " + a.equals(b) ); //trueString a = "abc"; String b = new String("abc"); System.out.println("a==b : " + (a==b)); //falseSystem.out.println("a.equals(b)  : " + a.equals(b) ); //true}/** * 4)JVM對於字串常量的"+"號串連,編譯期就將常量字串的"+"串連最佳化為串連後的值。 * 5)如果在字串+操作過程中,有引用存在,如t="a"; b = t+"bc";中的t,則運行期才能獲得 t記憶體塊,顯然不會最佳化為"abc"常量。但在變數前加個final,也會在編譯期儲存為常量池中或嵌入 到它的位元組碼流中。 */static void test_3(){String a = "abc";  String b = "a"+"bc";System.out.println("a==b : " + (a==b)); //trueSystem.out.println("a.equals(b)  : " + a.equals(b) ); //true//String a = "abc"; //String t = "a"; //String b = t+"bc";//System.out.println("a==b : " + (a==b)); //false//System.out.println("a.equals(b)  : " + a.equals(b) ); //true//String a = "abc"; //final String t="a"; //String b = t+"bc";//System.out.println("a==b : " + (a==b)); //true//System.out.println("a.equals(b)  : " + a.equals(b) ); //true}static void test_4(){String str="1234567890上海";int len = str.length();System.out.println("len = " + len);String str2 = "Hello," + " java" + "! " + len;System.out.println("str2 = " + str2);}/** * String、StringBuilder 1) String所引用空間扔掉,開闢新空間來存放字串(不變類),會出現記憶體片段; 2) StringBuilder先開闢了緩衝區,append()在緩衝區中操作,記憶體效率大大提高(可變類)。 */static void test_5(){System.out.println("運行前可用記憶體"+ Runtime.getRuntime().freeMemory());String s = "";String newStr = new String("abc");StringBuilder builder = new StringBuilder(1000);for (int i = 0; i < 100; i++) {//   s = s + newStr; //可用記憶體保持下降   builder.append(newStr);  //可用記憶體保持不變   if (i % 10 == 0) {          System.out.println("運行中可用記憶體"+ Runtime.getRuntime().freeMemory());   }}}/** * StringBuilder、StringBuffer 1) 相似點:做字串串連時,都不會產生記憶體片段; 2) 差別:StringBuilder類不是安全執行緒的,但其在單線程中的效能比StringBuffer高。 */static void test_6(){StringBuilder strBuilder = new StringBuilder();String str = new String();StringBuffer strBuffer = new StringBuffer();System.out.println( System.currentTimeMillis());for (int i = 0; i < 1000000; i++) {//str = str + " add ";//    strBuilder = strBuilder.append(" add ");    strBuffer = strBuffer.append(" add ");}System.out.println( System.currentTimeMillis());}public static void main(String[] args) {test_6();}}

   

 

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.