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".
We can convert a char to a string object in java by using the Character. toString() method.
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)
Integer class has static method toString() - you can use it:
int i = 1234; String str = Integer.toString(i);
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With