There is also Integer.toString(int i), which gives you the option of getting the string as a hex value as well (by passing a second param of 16).
Edit I just checked the source of String class:
public static String valueOf(int i) {
return Integer.toString(i, 10);
}
And Integer class:
public static String toString(int i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
...
If you call String.valueOf(i)
, it calls Integer.toString(i, 10)
, which then calls Integer.toString(i)
.
So Integer.toString(i)
should be very slighty faster than String.valueOf(i)
, since you'd be cutting out two function calls. (Although the first function call could be optimized away by the compiler.)
Of course, a readability argument could still be made for String.valueOf()
, since it allows you to change the type of the argument (and even handles nulls!), and the performance difference is negligible.