How do I convert from int to String?

后端 未结 20 2748
不思量自难忘°
不思量自难忘° 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:17

    Personally, I don't see anything bad in this code.

    It's pretty useful when you want to log an int value, and the logger just accepts a string. I would say such a conversion is convenient when you need to call a method accepting a String, but you have an int value.

    As for the choice between Integer.toString or String.valueOf, it's all a matter of taste.
    ...And internally, the String.valueOf calls the Integer.toString method by the way. :)

提交回复
热议问题