Android StringBuilder vs String Concatenation

前端 未结 2 663
一整个雨季
一整个雨季 2021-02-05 14:24

I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html.

The section here caught my eye:

Tip: Don\'t f

2条回答
  •  独厮守ぢ
    2021-02-05 14:35

    The compiler does exactly what you suggest is implied. You can print the bytecodes of the generated .class file (using javap -c) and see the calls to construct and use a StringBuilder.

    However, it's generally worth doing it manually when the string concatenations are spread over several lines of code. The compiler usually allocates a separate StringBuilder for every String-valued expression involving +.

提交回复
热议问题