Is concatenating with an empty string to do a string conversion really that bad?

后端 未结 9 958
别那么骄傲
别那么骄傲 2020-12-03 04:45

Let\'s say I have two char variables, and later on I want to concatenate them into a string. This is how I would do it:

char c1, c2;
// ...

Str         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 04:56

    The best way to know is to compile / decompile your code, I used Jad http://en.wikipedia.org/wiki/JAD_(JAva_Decompiler) for that, you will see that your expression was converted into

    String s = (new StringBuilder()).append("").append(ci).append(c2).toString();

    As you can see javac actually included append("") call, but its cost is negligible, noting is appended to internal StringBuilder buffer, you can check StringBuilder's source

提交回复
热议问题