How do I convert from int to String?

后端 未结 20 2747
不思量自难忘°
不思量自难忘° 2020-11-21 23:40

I\'m working on a project where all conversions from int to String are done like this:

int i = 5;
String strI = \"\" + i;


        
相关标签:
20条回答
  • 2020-11-22 00:24

    There are three ways of converting to Strings

    1. String string = "" + i;
    2. String string = String.valueOf(i);
    3. String string = Integer.toString(i);
    0 讨论(0)
  • 2020-11-22 00:26

    The other way I am aware of is from the Integer class:

    Integer.toString(int n);
    Integer.toString(int n, int radix);
    

    A concrete example (though I wouldn't think you need any):

    String five = Integer.toString(5); // returns "5"
    

    It also works for other primitive types, for instance Double.toString.

    See here for more details.

    0 讨论(0)
提交回复
热议问题