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

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

    I would also use String.valueOf() method, which, in effect, uses the primitive type's Wrapper object and calls the toString() method for you:

    Example, in the String class:

    public static String valueOf(int i) {
            return Integer.toString(i, 10);
        }
    

提交回复
热议问题