What is the better approach to convert primitive data type into String

后端 未结 9 572
独厮守ぢ
独厮守ぢ 2021-02-06 21:09

I can convert an integer into string using

String s = \"\" + 4; // correct, but poor style
or
String u = Integer.toString(4); // this is good

I

9条回答
  •  隐瞒了意图╮
    2021-02-06 21:28

    It is always better that you're aware of the type of argument you are trying to convert to string and also make compiler aware of the type. That simplifies the operation as well as the cycles. When you follow the append method, you are leaving the type decision to the compiler and also increasing the code lines for the compiler to do the same.

提交回复
热议问题