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

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

    There's a third one - String.valueOf(..) (which calls Wrapper.toString(..)

    Actually the compiler adds Wrapper.toString(..) in these places, so it's the same in terms of bytecode. But ""+x is uglier.

提交回复
热议问题