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

后端 未结 9 588
独厮守ぢ
独厮守ぢ 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:36

    String s = "" + 4;
    

    Is compiled to this code:

    StringBuffer _$_helper = new StringBuffer("");
    _$_helper.append(Integer.toString(4));
    String s = _$_helper.toString();
    

    Obviously that is pretty wastefull. Keep in mind that behind the scene the compiler is always using StringBuffers if you use + in asociation with String's

提交回复
热议问题