Java - Convert integer to string

Trufa picture Trufa · Feb 21, 2011 · Viewed 1.6M times · Source

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

Answer

Bozho picture Bozho · Feb 21, 2011

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)