Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Convert integer to string [duplicate]

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".

like image 703
Trufa Avatar asked Feb 21 '11 20:02

Trufa


People also ask

Can Char be converted to string?

We can convert a char to a string object in java by using the Character. toString() method.


2 Answers

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)
like image 56
Bozho Avatar answered Oct 21 '22 17:10

Bozho


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.

like image 38
lukastymo Avatar answered Oct 21 '22 19:10

lukastymo