關(guān)于JDK8中的字符串拼接示例詳解
前言
在Java開(kāi)發(fā)者中,字符串的拼接占用資源高往往是熱議的話(huà)題.
讓我們深入討論一下為什么會(huì)占用高資源。
在Java中,字符串對(duì)象是不可變的,意思是它一旦創(chuàng)建,你就無(wú)法再改變它。所以在我們拼接字符串的時(shí)候,創(chuàng)建了一個(gè)新的字符串,舊的被垃圾回收器所標(biāo)記。
如果我們處理上百萬(wàn)的字符串,然后,我們就會(huì)生成百萬(wàn)的額外字符串被垃圾回收器處理。
在大多數(shù)的教程中,也許你會(huì)看到用+號(hào)拼接字符串會(huì)生成多個(gè)String,導(dǎo)致性能過(guò)差,建議使用StringBuffer/StringBuilder來(lái)拼接。
可是真的是這樣的嗎?
本文在JDK8中做了如下實(shí)驗(yàn):
public static void main(String[] args) { String result = ""; result += "some more data"; System.out.println(result); }
通過(guò)javap -c來(lái)反編譯得到:
Code: 0: aload_0 // Push 'this' on to the stack 1: invokespecial #1 // Invoke Object class constructor // pop 'this' ref from the stack 4: return // Return from constructor public static void main(java.lang.String[]); Code: 0: ldc #2 // Load constant #2 on to the stack 2: astore_1 // Create local var from stack (pop #2) 3: new #3 // Push new StringBuilder ref on stack 6: dup // Duplicate value on top of the stack 7: invokespecial #4 // Invoke StringBuilder constructor // pop object reference 10: aload_1 // Push local variable containing #2 11: invokevirtual #5 // Invoke method StringBuilder.append() // pop obj reference + parameter // push result (StringBuilder ref) 14: ldc #6 // Push "some more data" on the stack 16: invokevirtual #5 // Invoke StringBuilder.append // pop twice, push result 19: invokevirtual #7 // Invoke StringBuilder.toString:(); 22: astore_1 // Create local var from stack (pop #6) 23: getstatic #8 // Push value System.out:PrintStream 26: aload_1 // Push local variable containing #6 27: invokevirtual #9 // Invoke method PrintStream.println() // pop twice (object ref + parameter) 30: return // Return void from method
可以看到Java編譯器優(yōu)化了生成的字節(jié)碼,自動(dòng)創(chuàng)建了一個(gè)StringBuilder,并進(jìn)行append操作。
由于構(gòu)建最終字符串的子字符串在編譯時(shí)已經(jīng)已知了,在這種情況下Java編譯器才會(huì)進(jìn)行如上的優(yōu)化。這種優(yōu)化稱(chēng)為a static string concatenation optimization,自JDK5時(shí)就開(kāi)始啟用。
那是否就能說(shuō)明在JDK5以后,我們不再需要手動(dòng)生成StringBuilder,通過(guò)+號(hào)也能達(dá)到同樣的性能?
我們嘗試下動(dòng)態(tài)拼接字符串:
動(dòng)態(tài)拼接字符串指的是僅在運(yùn)行時(shí)才知道最終字符串的子字符串。比如在循環(huán)中增加字符串:
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 stack 1: invokespecial #1 // Invoke Object class constructor // pop 'this' ref from the stack 4: return // Return from constructor public static void main(java.lang.String[]); Code: 0: ldc #2 // Load constant #2 on to the stack 2: astore_1 // Create local var from stack, pop #2 3: iconst_0 // Push value 0 onto the stack 4: istore_2 // Pop value and store it in local var 5: iload_2 // Push local var 2 on to the stack 6: i2d // Convert int to double on // top of stack (pop + push) 7: ldc2_w #3 // Push constant 10e6 on to the stack 10: dcmpg // Compare two doubles on top of stack // pop twice, push result: -1, 0 or 1 11: ifge 40 // if value on top of stack is greater // than or equal to 0 (pop once) // branch to instruction at code 40 14: new #5 // Push new StringBuilder ref on stack 17: dup // Duplicate value on top of the stack 18: invokespecial #6 // Invoke StringBuilder constructor // pop object reference 21: aload_1 // Push local var 1 (empty String) // on to the stack 22: invokevirtual #7 // Invoke StringBuilder.append // pop obj ref + param, push result 25: ldc #8 // Push "some more data" on the stack 27: invokevirtual #7 // Invoke StringBuilder.append // pop obj ref + param, push result 30: invokevirtual #9 // Invoke StringBuilder.toString // pop object reference 33: astore_1 // Create local var from stack (pop) 34: iinc 2, 1 // Increment local variable 2 by 1 37: goto 5 // Move to instruction at code 5 40: getstatic #10 // Push value System.out:PrintStream 43: 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的時(shí)候new了StringBuilder,但是在37的時(shí)候goto到了5,在循環(huán)過(guò)程中,并沒(méi)有達(dá)到最優(yōu)化,不斷在生成新的StringBuilder。
所以上述代碼類(lèi)似:
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,并且通過(guò)tostring,原來(lái)的StringBuilder將不再引用,作為垃圾,也增加了GC成本。
所以,在實(shí)際的使用中,當(dāng)你無(wú)法區(qū)分字符串是靜態(tài)拼接還是動(dòng)態(tài)拼接的時(shí)候,還是使用StringBuilder吧。
Reference:
http://www.pellegrino.link/2015/08/22/string-concatenation-with-java-8.html
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring Boot 配置 IDEA和DevTools 熱部署的方法
這篇文章主要介紹了Spring Boot 配置 IDEA和DevTools 熱部署的方法,需要的朋友可以參考下2018-02-02mybatis的動(dòng)態(tài)SQL和模糊查詢(xún)實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于mybatis的動(dòng)態(tài)SQL和模糊查詢(xún)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Spring Boot2.0中SpringWebContext找不到無(wú)法使用的解決方法
這篇文章主要給大家介紹了關(guān)于Spring Boot2.0中SpringWebContext找不到無(wú)法使用的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12解決Spring在Thread中注入Bean無(wú)效的問(wèn)題
這篇文章主要介紹了解決Spring在Thread中注入Bean無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線(xiàn)程池的特性,也可以實(shí)現(xiàn)任務(wù)循環(huán)執(zhí)行,本文主要介紹了Java ScheduledExecutorService的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05多jdk環(huán)境下指定springboot外部配置文件詳解
這篇文章主要為大家介紹了多jdk環(huán)境下指定springboot外部配置文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語(yǔ)句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06教你如何精準(zhǔn)統(tǒng)計(jì)出你的接口"QPS"
今天小編就為大家分享一篇關(guān)于QPS的精準(zhǔn)計(jì)算方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2021-08-08