谈谈JDK8中的字符串拼接
字符串拼接問題應該是每個Java程序員都熟知的事情了,幾乎每個Java程序員都讀過關于StringBuffer/StringBuilder來拼接字符串。
在大多數的教程中,也許你會看到用+號拼接字符串會生成多個String,導致性能過差,建議使用StringBuffer/StringBuilder來拼接。
可是真的是這樣的嗎?
本文在JDK8中做了如下實驗:
public static void main(String[] args) {String result = "";result += "some more data";System.out.println(result);}
通過javap -c來反編譯得到:
Code:0: aload_0 // Push 'this' on to the stack1: invokespecial #1 // Invoke Object class constructor// pop 'this' ref from the stack4: return // Return from constructorpublic static void main(java.lang.String[]);Code:0: ldc #2 // Load constant #2 on to the stack2: astore_1 // Create local var from stack (pop #2)3: new #3 // Push new StringBuilder ref on stack6: dup // Duplicate value on top of the stack7: invokespecial #4 // Invoke StringBuilder constructor// pop object reference10: aload_1 // Push local variable containing #211: invokevirtual #5 // Invoke method StringBuilder.append()// pop obj reference + parameter// push result (StringBuilder ref)14: ldc #6 // Push "some more data" on the stack16: invokevirtual #5 // Invoke StringBuilder.append// pop twice, push result19: invokevirtual #7 // Invoke StringBuilder.toString:();22: astore_1 // Create local var from stack (pop #6)23: getstatic #8 // Push value System.out:PrintStream26: aload_1 // Push local variable containing #627: invokevirtual #9 // Invoke method PrintStream.println()// pop twice (object ref + parameter)30: return // Return void from method
可以看到Java編譯器優化了生成的字節碼,自動創建了一個StringBuilder,并進行append操作。
由于構建最終字符串的子字符串在編譯時已經已知了,在這種情況下Java編譯器才會進行如上的優化。這種優化稱為a static string concatenation optimization,自JDK5時就開始啟用。
那是否就能說明在JDK5以后,我們不再需要手動生成StringBuilder,通過+號也能達到同樣的性能?
我們嘗試下動態拼接字符串:
動態拼接字符串指的是僅在運行時才知道最終字符串的子字符串。比如在循環中增加字符串:
public static void main(String[] args) {String result = "";for (int i = 0; i < 10; i++) {result += "some more data";}System.out.println(result);}
同樣反編譯:
Code:0: aload_0 // Push 'this' on to the stack1: invokespecial #1 // Invoke Object class constructor// pop 'this' ref from the stack4: return // Return from constructorpublic static void main(java.lang.String[]);Code:0: ldc #2 // Load constant #2 on to the stack2: astore_1 // Create local var from stack, pop #23: iconst_0 // Push value 0 onto the stack4: istore_2 // Pop value and store it in local var5: iload_2 // Push local var 2 on to the stack6: i2d // Convert int to double on// top of stack (pop + push)7: ldc2_w #3 // Push constant 10e6 on to the stack10: dcmpg // Compare two doubles on top of stack// pop twice, push result: -1, 0 or 111: ifge 40 // if value on top of stack is greater// than or equal to 0 (pop once)// branch to instruction at code 4014: new #5 // Push new StringBuilder ref on stack17: dup // Duplicate value on top of the stack18: invokespecial #6 // Invoke StringBuilder constructor// pop object reference21: aload_1 // Push local var 1 (empty String)// on to the stack22: invokevirtual #7 // Invoke StringBuilder.append// pop obj ref + param, push result25: ldc #8 // Push "some more data" on the stack27: invokevirtual #7 // Invoke StringBuilder.append// pop obj ref + param, push result30: invokevirtual #9 // Invoke StringBuilder.toString// pop object reference33: astore_1 // Create local var from stack (pop)34: iinc 2, 1 // Increment local variable 2 by 137: goto 5 // Move to instruction at code 540: getstatic #10 // Push value System.out:PrintStream43: aload_1 // Push local var 1 (result String)44: invokevirtual #11 // Invoke method PrintStream.println()// pop twice (object ref + parameter)47: return // Return void from method
可以看到在14的時候new了StringBuilder,但是在37的時候goto到了5,在循環過程中,并沒有達到最優化,不斷在生成新的StringBuilder。
所以上述代碼類似:
String result = ""; for (int i = 0; i < 10; i++) {StringBuilder tmp = new StringBuilder();tmp.append(result);tmp.append("some more data");result = tmp.toString(); } System.out.println(result);
可以看到不斷生成新的StringBuilder,并且通過tostring,原來的StringBuilder將不再引用,作為垃圾,也增加了GC成本。
所以,在實際的使用中,當你無法區分字符串是靜態拼接還是動態拼接的時候,還是使用StringBuilder吧。
Reference:
http://www.pellegrino.link/2015/08/22/string-concatenation-with-java-8.html
來源:開源中國---Hosee
鏈接:https://my.oschina.net/hosee/blog/1786130
更多干貨可關注公眾號,回復“文檔”獲取
轉載于:https://www.cnblogs.com/xdclass/p/9789557.html
總結
以上是生活随笔為你收集整理的谈谈JDK8中的字符串拼接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: the params of sys au
- 下一篇: SPSS Clementine 安装教程