何時在Java中使用StringBuilder [重複]

本文翻譯自:When to use StringBuilder in Java [duplicate]

This question already has an answer here: 這個問題在這裏已有答案:

It is supposed to be generally preferable to use a StringBuilder for string concatenation in Java. 通常最好在Java中使用StringBuilder進行字符串連接。 Is this always the case? 總是這樣嗎?

What I mean is this: Is the overhead of creating a StringBuilder object, calling the append() method and finally toString() already smaller then concatenating existing strings with the + operator for two strings, or is it only advisable for more (than two) strings? 我的意思是這樣的:創建一個StringBuilder對象,調用append()方法和最後toString()的開銷已經小了,然後用兩個字符串的+運算符連接現有的字符串,或者只建議更多(兩個以上) )字符串?

If there is such a threshold, what does it depend on (perhaps the string length, but in which way)? 如果有這樣的閾值,它依賴於什麼(可能是字符串長度,但以哪種方式)?

And finally, would you trade the readability and conciseness of the + concatenation for the performance of the StringBuilder in smaller cases like two, three or four strings? 最後,將您的交易的可讀性和簡潔+拼接的性能StringBuilder在較小的情況下,像二,三或四根弦?

Explicit use of StringBuilder for regular concatenations is being mentioned as obsolete at obsolete Java optimization tips as well as at Java urban myths . 過時的Java優化技巧以及Java都市神話中,已經提到明確使用StringBuilder進行常規連接。


#1樓

參考:https://stackoom.com/question/JUNg/何時在Java中使用StringBuilder-重複


#2樓

對於兩個字符串,concat更快,在其他情況下,StringBuilder是更好的選擇,請參閱我在串聯運算符(+)vs concat()中的解釋


#3樓

Some compilers may not replace any string concatenations with StringBuilder equivalents. 某些編譯器可能無法用StringBuilder等效替換任何字符串連接。 Be sure to consider which compilers your source will use before relying on compile time optimizations. 在依賴編譯時優化之前,請務必考慮源代碼將使用哪些編譯器。


#4樓

The Microsoft certification material addresses this same question. Microsoft認證材料解決了同樣的問題。 In the .NET world, the overhead for the StringBuilder object makes a simple concatenation of 2 String objects more efficient. 在.NET世界中,StringBuilder對象的開銷使得2個String對象的簡單連接更加高效。 I would assume a similar answer for Java strings. 我會假設Java字符串的類似答案。


#5樓

Have a look at: http://www.javaspecialists.eu/archive/Issue068.html and http://www.javaspecialists.eu/archive/Issue105.html 請查看: http//www.javaspecialists.eu/archive/Issue068.htmlhttp://www.javaspecialists.eu/archive/Issue105.html

Do the same tests in your environment and check if newer JDK or your Java implementation do some type of string operation better with String or better with StringBuilder . 在您的環境中執行相同的測試,並檢查較新的JDK或Java實現是否使用String或使用StringBuilder更好地執行某種類型的字符串操作。


#6樓

As a general rule, always use the more readable code and only refactor if performance is an issue. 作爲一般規則,始終使用更易讀的代碼,並且只有在性能成爲問題時才重構。 In this specific case, most recent JDK's will actually optimize the code into the StringBuilder version in any case. 在這種特定情況下,最新的JDK實際上將在任何情況下將代碼優化爲StringBuilder版本。

You usually only really need to do it manually if you are doing string concatenation in a loop or in some complex code that the compiler can't easily optimize. 如果您在循環中或在編譯器無法輕鬆優化的某些複雜代碼中進行字符串連接,通常只需要手動執行此操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章