I tried the following program
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final int iterCount = 1000000;
// test String.valueOf
long start = System.currentTimeMillis();
for (int i = 0; i < iterCount; i++) {
String s = String.valueOf(i);
}
System.out.println("DEBUG: String.valueOf took "
+ (System.currentTimeMillis() - start) + " ms for "
+ iterCount + " iterations");
// test Integer.toString
start = System.currentTimeMillis();
for (int i = 0; i < iterCount; i++) {
String s = Integer.toString(i);
}
System.out.println("DEBUG: Integer.toString took "
+ (System.currentTimeMillis() - start) + " ms for "
+ iterCount + " iterations");
// test indirect conversion
start = System.currentTimeMillis();
for (int i = 0; i < iterCount; i++) {
String s = "" + i;
}
System.out.println("DEBUG: Indirect string conversion took "
+ (System.currentTimeMillis() - start) + " ms for "
+ iterCount + " iterations");
}
}
Output
DEBUG: String.valueOf took 106 ms for 1000000 iterations
DEBUG: Integer.toString took 92 ms for 1000000 iterations
DEBUG: Indirect string conversion took 271 ms for 1000000 iterations
So the summary is that Integer.toString provides the best performance.