This question already has an answer here:
Given a number:
int number = 1234;
Which would be the "best" way to convert this to a string:
String stringNumber = "1234";
I have tried searching (googling) for an answer but no many seemed "trustworthy".
This will do. Pretty trustworthy. : )
Just to clarify, this works and acceptable to use unless you are looking for micro optimization.
Always use either
String.valueOf(number)
orInteger.toString(number)
.Using "" + number is an overhead and does the following:
The way I know how to convert an integer into a string is by using the following code:
and
If you had an integer i, and a string s, then the following would apply:
If you wanted to convert a string "s" into an integer "i", then the following would work:
This is the method which i used to convert the integer to string.Correct me if i did wrong.
Integer class has static method toString() - you can use it:
There are multiple ways:
String.valueOf(number)
(my preference)"" + number
(I don't know how the compiler handles it, perhaps it is as efficient as the above)Integer.toString(number)