JAVA 中int类型转String类型的通常方法,有三种:
from http://sdylag.iteye.com/blog/932559
JAVA 中int类型转String类型的通常方法,有三种:
1、String.valueOf(int i)
2、Integer.toString(int i)
3、i+""; //i 为 int类型
三种方法效率排序为:
Integer.toString(int i) > String.valueOf(int i) > i+"";
代码测试:
public static void main(String[] args) {
int[] intArr = new int[1000000];
String[] strArr1 = new String[1000000];//为了公平分别定义三个数组
String[] strArr2 = new String[1000000];
String[] strArr3 = new String[1000000];
//赋值
Long t1 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
intArr[i]=i+1;
}
Long t2 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
strArr1[i] = String.valueOf(intArr[i]);
}
Long t3 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
strArr2[i] = Integer.toString(intArr[i]);
}
Long t4 = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
strArr3[i] = intArr[i]+"";
}
Long t5 = System.currentTimeMillis();
System.out.println("t1 = "+t1);
System.out.println("t2 = "+t2);
System.out.println("t3 = "+t3);
System.out.println("t4 = "+t4);
System.out.println("t5 = "+t5);
System.out.println("赋值:"+(t2-t1));
System.out.println("String.valueOf(i):"+(t3-t2));
System.out.println("Integer.toString(i):"+(t4-t3));
System.out.println("i+\"\":"+(t5-t4));
}
我的测试结果为:
t1 = 1298798872078
t2 = 1298798872093
t3 = 1298798872656
t4 = 1298798873000
t5 = 1298798873671
赋值:15
String.valueOf(i):563
Integer.toString(i):344
i+"":671